Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
逻辑错误Java-BMI中的计算错误_Java_If Statement - Fatal编程技术网

逻辑错误Java-BMI中的计算错误

逻辑错误Java-BMI中的计算错误,java,if-statement,Java,If Statement,该程序允许您输入用户的身高和体重,然后输出BMI和相关的健康风险。它把磅换算成公斤。它还将以英尺和英寸为单位的高度转换为米 Scanner scanW = new Scanner (System.in); Scanner scanH = new Scanner (System.in); System.out.println("Enter your weight in pounds: "); int weight = scanW.nex

该程序允许您输入用户的身高和体重,然后输出BMI和相关的健康风险。它把磅换算成公斤。它还将以英尺和英寸为单位的高度转换为米

    Scanner scanW = new Scanner (System.in);
    Scanner scanH = new Scanner (System.in);
    
    System.out.println("Enter your weight in pounds: ");
    int weight = scanW.nextInt();
    
    System.out.println("Enter your height in feet followed \nBy a space then additional inches");
    String height = scanH.nextLine();

    scanW.close();
    scanH.close();
    
    int heightFt = height.charAt(0);
    int heightInch = height.charAt(2);
    
    int finalHeightFeet = Integer.parseInt(String.valueOf(heightFt));
    int finalHeightInch = Integer.parseInt(String.valueOf(heightInch));
    
    double mass = (double) weight / 2.2;
    double finalHeight = (double) (finalHeightFeet * 0.3048) + (finalHeightInch * 0.0254);
    double BMI = (double) mass / (finalHeight * finalHeight);
    System.out.println("Your BMI is " +BMI);
    
    if (BMI < 18.5)
        System.out.println("Your risk category is UnderWeight");
    else if (BMI < 25)
        System.out.println("Your risk category is Normal Weight");
    else if (BMI < 30)
        System.out.println("Your risk category is Normal Overweight");
    else if (BMI >= 30)
        System.out.println("Your risk category is Obese");
    
但我的输出是这样的:

Your BMI is 0.22261924276759873
Your risk category is UnderWeight

我很确定我的公式有问题,但我似乎找不到。如果有人指出哪一个是错误的,这将非常有帮助

您没有正确解析高度输入

假设你打字

5 9
和高度一样

将5 9指定为高度

然后解析

int heightFt = height.charAt(0);
int heightInch = height.charAt(2);    
它将53指定给heightFt,57指定给heightInch,这是字符“5”和“9”的数值

请尝试以下方法:

String[] height = scanH.nextLine().split (" ");
int heightFt = Integer.parseInt (height[0]);
int heightInch = Integer.parseInt (height[1]);

您没有正确分析高度输入

假设你打字

5 9
和高度一样

将5 9指定为高度

然后解析

int heightFt = height.charAt(0);
int heightInch = height.charAt(2);    
它将53指定给heightFt,57指定给heightInch,这是字符“5”和“9”的数值

请尝试以下方法:

String[] height = scanH.nextLine().split (" ");
int heightFt = Integer.parseInt (height[0]);
int heightInch = Integer.parseInt (height[1]);
您正在解析以数值表示的字符。 看一看

例如,如果将6 2作为高度,结果实际上是6为54,空格为32,2为50。

您正在解析以数值表示的字符。 看一看

例如,如果将6 2作为高度,结果实际上是6为54,空间为32,2为50

我修改了你的代码,看一看

Scanner scanW = new Scanner (System.in);
Scanner scanH = new Scanner (System.in);

System.out.println("Enter your weight in pounds: ");
int weight = scanW.nextInt();

System.out.println("Enter your height in feet followed \nBy a space then additional inches");
String height = scanH.nextLine();

int feets = Integer.parseInt(height.substring(0,1));

// get all the chars after index 2
int inches = Integer.parseInt(height.substring(2));

int totalInches = feets * 12 + inches;
double BMI = weight/Math.pow(totalInches, 2) * 703;
System.out.println("BMI: "+BMI);
我修改了你的代码,看一看

Scanner scanW = new Scanner (System.in);
Scanner scanH = new Scanner (System.in);

System.out.println("Enter your weight in pounds: ");
int weight = scanW.nextInt();

System.out.println("Enter your height in feet followed \nBy a space then additional inches");
String height = scanH.nextLine();

int feets = Integer.parseInt(height.substring(0,1));

// get all the chars after index 2
int inches = Integer.parseInt(height.substring(2));

int totalInches = feets * 12 + inches;
double BMI = weight/Math.pow(totalInches, 2) * 703;
System.out.println("BMI: "+BMI);

美国单位:体重指数=重量磅÷高度2英寸*703。这是一个公式。不要为system.in初始化两个扫描仪,一个就足够了。关闭其中一个将使system.in中的任何其他扫描仪永久失效。在你的例子中,这是无害的,但这可能有助于未来的案例单位:BMI=体重磅÷身高2英寸*703。这是一个公式。不要为system.in初始化两个扫描仪,一个就足够了。关闭其中一个将使system.in中的任何其他扫描仪永久失效。在您的示例中,这并不有害,但这可能对将来的案例有所帮助