的「你是正常體重」和「你是超重」將不顯示不工作。 我的代碼:如果其他如預期
import java.util.Scanner;
public class BodyMassIndex
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double bmi, weight, height;
System.out.println("----LETS CHECK YOUR BMI----");
System.out.print("Enter your weight (kg): ");
weight = scan.nextDouble();
System.out.print("Enter your height (m): ");
height = scan.nextDouble();
System.out.println("-----------RESULTS----------");
bmi = weight/(height*height);
System.out.printf("%-10s%-4.2f" , "\t\t Your BMI is " , bmi);
if(bmi<18.5)
{System.out.println("\n\t\t You are UNDERWIGHT");}
else if(bmi==18.5 && bmi <= 24.9)
{System.out.println(" You are NORMAL WEIGHT");}
else if(bmi==25 && bmi <= 29.9)
{System.out.println(" You are OVERWEIGHT");}
else if(bmi > 30)
{System.out.println("\n\t\t You are OBESE");}
else
{}
}
}
變化BMI == 18.5至BMI> = 18.5和BMI == 25至BMI> = 25 – Satya
只需卸下較低的值(無論之前是' &&'),因爲它們在前面的if語句中被處理了。 – AntonH