我很新的編程和我目前教自己的Java。我試圖製作一個涉及BMI的程序,但我繼續顯示0
而不是BMI值。Java - 數學與整數
我敢肯定我的錯誤是在數學,但我不知道是什麼改變:
public class bmi {
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
System.out.print("Weight(kg):");
int weight=k.nextInt();
//now for the second scanner or int
Scanner j = new Scanner(System.in);
System.out.print("Height (m):");
int height=j.nextInt();
double bodyMassIndex = ((double) weight/(height * height));
//so far this looks fine i believe , however there might be a problem with the math.
System.out.println(bodyMassIndex);
}
}
整數除法正在截斷。使重量和高度變量加倍。 –