所以我正在制作一个BMI计算器,为用户提供英制和公制之间的选项。但是,无论何时计算英制bmi,它都会返回0.我非常确定这与未保存的值有关。
import java.util.Scanner;
public class BMICalculator {
public static double lbWeight = 0;
public static double inchHeight = 0;
public static double kgWeight = 0;
public static double mHeight = 0;
public static double imperialBMI = 0;
public static double metricBMI = 0;
public static String userName;
public static void programIntroduction()
{
System.out.print("Welcome to the BMI Calculator, please enter your name:");
Scanner scannerName = new Scanner(System.in);
userName = scannerName.nextLine();
}
public static void getLBWeight()
{
System.out.print("Please enter your weight from 22 to 660 LB:");
Scanner scannerWeight = new Scanner(System.in);
lbWeight = scannerWeight.nextDouble();
for (;lbWeight<22;)
{
System.out.println("Please enter a valid weight:");
Scanner scannerWeight2 = new Scanner(System.in);
lbWeight = scannerWeight2.nextDouble();
}
for (;lbWeight>660;)
{
System.out.println("Please enter a valid weight:");
Scanner scannerWeight2 = new Scanner(System.in);
lbWeight = scannerWeight2.nextDouble();
}
}
public static void getInchHeight()
{
System.out.print("Please enter your height from 20 to 120 inches:");
Scanner scannerHeight = new Scanner(System.in);
inchHeight = scannerHeight.nextDouble();
for (;inchHeight<20;)
{
System.out.println("Please enter a valid height:");
Scanner scannerHeight2 = new Scanner(System.in);
inchHeight = scannerHeight2.nextDouble();
}
for (;inchHeight>120;)
{
System.out.println("Please enter a valid height:");
Scanner scannerHeight2 = new Scanner(System.in);
inchHeight = scannerHeight2.nextDouble();
}
}
public static void getKGWeight()
{
System.out.print("Please enter your weight from 10 to 300 KG:");
Scanner scannerWeightKG = new Scanner(System.in);
kgWeight = scannerWeightKG.nextInt();
for (;kgWeight<10;)
{
System.out.println("Please enter a valid weight:");
Scanner scannerWeight2 = new Scanner(System.in);
kgWeight = scannerWeight2.nextInt();
}
for (;kgWeight>300;)
{
System.out.println("Please enter a valid weight:");
Scanner scannerWeight2 = new Scanner(System.in);
kgWeight = scannerWeight2.nextInt();
}
}
public static void getMHeight()
{
System.out.print("Please enter your height from .5 to 3 M:");
Scanner scannerHeightM = new Scanner(System.in);
mHeight = scannerHeightM.nextInt();
for (;mHeight<.5;)
{
System.out.println("Please enter a valid weight:");
Scanner scannerHeight2 = new Scanner(System.in);
mHeight = scannerHeight2.nextInt();
}
for (;mHeight>3;)
{
System.out.println("Please enter a valid weight:");
Scanner scannerHeight2 = new Scanner(System.in);
mHeight = scannerHeight2.nextInt();
}
}
public static void ImperialBMI()
{
imperialBMI = (lbWeight*703)/(inchHeight*inchHeight);
}
public static void MetricBMI()
{
metricBMI = (kgWeight)/(mHeight*mHeight);
}
public static void main(String[] args)
{
programIntroduction();
System.out.println("Please type 1 for imperial mode or 2 for metric mode:");
Scanner scanner = new Scanner(System.in);
int userInput = scanner.nextInt();
if (userInput == 1)
{
getLBWeight();
getInchHeight();
System.out.println(imperialBMI);
}
if (userInput == 2)
{
getKGWeight();
getMHeight();
System.out.println(metricBMI);
}
}
}
答案 0 :(得分:3)
您已将imperialBMI
和metricBMI
初始化为0
,但您从未调用计算其值的方法。在输出相应的变量之前,请先调用MetricBMI()
或ImperialBMI()
。