基于预期结果的错误数字(BMI w/ Java)

问题描述 投票:0回答:1

出于我自己的好奇心,我在朋友的代码上问这个问题。

它获取用户输入并使用它计算 BMI。虽然数字出现错误,而且我不懂 java,所以我很想知道为什么。

要遵循的代码:

import java.util.Scanner;

public class ComputeBMI{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);

        final double INCHES_PER_FEET = 12; // Constant
     
        System.out.printf("Enter weight in pounds: ");
        double weight = in.nextDouble();
        System.out.printf("Enter feet: ");
        int feet = in.nextInt();
        System.out.printf("Enter inches: ");
        int inches = in.nextInt();

        double totalHeight = (feet * INCHES_PER_FEET) + inches;
        double bmi = (weight /(totalHeight * totalHeight)) * 720;



        System.out.println("BMI is " + bmi);

        if (bmi < 18.5)
            System.out.println("Underweight");
        else if (bmi < 25)
            System.out.println("Normal");
        else if (bmi < 30)
            System.out.println("Overweight");
        else
            System.out.println("Obese");

身高 5 英尺 10 英寸,体重 140 磅的用户的输出结果如下:

Enter weight in pounds: 140
Enter feet: 5
Enter inches: 10
BMI is 984.297411494823
Obese

应该在20左右,这很正常

谁能告诉我我朋友做错了什么?

谢谢。

java user-input
1个回答
-1
投票

代码似乎对我有用。我复制并粘贴它并运行它以获得 20.57142857142857 的结果。我唯一改变的是在代码末尾添加两个右括号。一切看起来也写得正确。

Code output

© www.soinside.com 2019 - 2024. All rights reserved.