BMI计算器Java输出不正确

时间:2017-07-12 00:42:23

标签: java

我正在尝试用Java编写基本命令行BMI计算器,并且出于某种原因每次运行代码并输入我的身高和体重时,输出0。请帮我理解我犯了错误的地方。

import java.util.Scanner;
public class Chap2 {
    public static void main(String[] args){
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter your height in inches: ");
        int myHeight = reader.nextInt();
        System.out.println("Enter your weight in lbs: ");
        int myWeight = reader.nextInt();
        int Bmi = (myWeight/myHeight/myHeight)*703;
        System.out.println("Your BMI is " + Bmi + ".");
}
}

我的输出如下:

Enter your height in inches: 
68
Enter your weight in lbs: 
180
Your BMI is 0.

1 个答案:

答案 0 :(得分:0)

您将intint分开,因此结果为四舍五入int。 在计算之前转换为双精度以获得精确结果。

double bmi = ((double) myWeight / myHeight / myHeight) * 703;