-3
因此,我製作了一個BMI計算器,該計算器爲用戶提供了英制和公制之間的選項。然而,無論什麼時候帝國bmi被計算出來,它都會返回爲0.我很確定這與未保存的值有關。BMI Calculator每次返回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);
}
}
}
您每次閱讀某些內容時都不需要創建一個新的「掃描儀」。你可以創建一個掃描器作爲靜態變量並使用它。 – 2014-09-19 16:23:02