Java 为什么我的所有布尔类型不能运行并给出单独的结果?

Java 为什么我的所有布尔类型不能运行并给出单独的结果?,java,Java,当你运行代码时,它每次只会告诉你你体重不足,即使BMI没有体重不足 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

当你运行代码时,它每次只会告诉你你体重不足,即使BMI没有体重不足

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();

    }
为什么我的所有布尔类型不能运行并给出单独的结果


有人能帮你解决这个问题吗?

你的代码一直处于重量不足状态的原因是因为你在那之后要求输入。请求输入将停止程序,直到您输入输入。请尝试执行以下代码:

 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();

        }

你没有跳过行分隔符。在尝试读取用户输入之前,您需要添加ScannernextLine。这是因为您以这种方式对其进行了编码。你的代码中没有任何条件,所以你的第一个print语句总是被执行,然后你要求用户输入并覆盖计算出的BMI值。我如何将BMI小数点四舍五入?