Discuss / JavaScript / 题目解答

题目解答

Topic source

小宋是呢

#1 Created at ... [Delete] [Delete and Lock User]
var bmi = weight/(height*height);
var msg = "";
if(bmi<18.5){
msg = "过轻";
}else if(bmi>=18.5&&bmi<25){
msg = "正常";
}else if(bmi>=25&&bmi<28){
msg = "过重";
}else if(bmi>=28&&bmi<32){
msg = "肥胖";
}else{
msg = "严重肥胖";
}
alert(bmi+msg);

Rarin陈

#2 Created at ... [Delete] [Delete and Lock User]

var bmi = weight/(height*height);

if (bmi < 18.5) {

console.log("你太瘦了!");

} else if (bmi < 25) {

console.log("你很正常!");

} else if (bmi < 28) {

console.log("你有点重了!")

} else if (bmi < 32) {

console.log("你很重了!");

} else {

console.log("你是真特么的重");

}

实际上,你没执行18.5就代表年已经大于了18.5那个值,所以你在后面都不必写多的

Dongb9527

#3 Created at ... [Delete] [Delete and Lock User]

没错,写的话太啰嗦了

大神我的哪错了,求解

var bmi = weight / (height * height);

console.log(bmi);

if (bmi < 18.5){

console.log('过轻');

}else if (bmi >= 18.5 && bmi < 25){

console.log('正常');

}else if (bmi >= 25 && bmi < 28){

console.log('过重');

}else if (bmi >= 28 && bmi < 32){

console.log('肥胖');

}

else{

console.log('严重肥胖');

}

中文分号

另外注意边界条件。如果没有输入输出应该控制下,否则按照undefine会直接进入最后一个else输出过度肥胖。

所以最好最后一个也要带上条件,然后所有意外输入归到一个alert里面去


  • 1

Reply