当你运行代码时,它只会告诉你每次体重都不足,即使体重指数不低于体重。
public class BMI_Calc
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("This program computes your BMI");
System.out.print("What is first your name? ");
String name = sc.next();
System.out.print("Enter your weight in pounds: ");
double weight = sc.nextDouble();
System.out.print("Enter your height in inches: ");
double height = sc.nextDouble();
double new_weight = weight*703;
double new_height = height * height;
double bmi = new_weight/new_height;
boolean isUnderweight;
boolean isNormalweight;
boolean isOverweight;
boolean isObese;
System.out.println("Dear " + name + ", your BMI is " + bmi +" and you are under weight.");
bmi = sc.nextDouble();
isUnderweight = (bmi<=18.5);
System.out.println("Dear " + name + " your BMI is " + bmi +" and you are normal weight.");
bmi = sc.nextDouble();
isNormalweight = (bmi>18.5&&bmi<24.9);
System.out.println("Dear " + name + " your BMI is " + bmi +" and you are over weight.");
bmi = sc.nextDouble();
isOverweight = (bmi>24.9&&bmi<29.9);
System.out.println("Dear " + name + " your BMI is " + bmi +" and you are obese.");
bmi = sc.nextDouble();
isObese = (bmi>=30.0);
sc.close();
}
为什么我的所有布尔类型都会运行并给出单独的结果?
任何人都可以帮忙解决这个问题吗?
答案 0 :(得分:0)
您的代码卡在体重不足的原因是因为您之后要求输入。要求输入会停止程序,直到您输入输入。请尝试改为使用此代码:
public class BMI_Calc.java
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("This program computes your BMI");
System.out.print("What is first your name? ");
String name = sc.next();
System.out.print("Enter your weight in pounds: ");
double weight = sc.nextDouble();
System.out.print("Enter your height in inches: ");
double height = sc.nextDouble();
double new_weight = weight*703;
double new_height = height * height;
double bmi = new_weight/new_height;
boolean isUnderweight = (bmi<=18.5);
boolean isNormalweight = (bmi>18.5&&bmi<24.9);
boolean isOverweight = (bmi>24.9&&bmi<29.9);
boolean isObese = (bmi>=30.0);
if(isUnderweight){
System.out.println("Dear " + name + ", your BMI is " + bmi +" and you are under weight.");
}else if(isNormalweight){
System.out.println("Dear " + name + " your BMI is " + bmi +" and you are normal weight.");
}else if(isOverweight){
System.out.println("Dear " + name + " your BMI is " + bmi +" and you are over weight.");
}else if(isObese){
System.out.println("Dear " + name + " your BMI is " + bmi +" and you are obese.");
}
sc.close();
}