2014-09-19 86 views
-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); 
    } 

} 

}

+0

您每次阅读某些内容时都不需要创建一个新的“扫描仪”。你可以创建一个扫描器作为静态变量并使用它。 – 2014-09-19 16:23:02

回答

3

您初始化imperialBMImetricBMI0,但你永远不调用计算其值的方法。在输出相应的变量之前,请拨打MetricBMI()ImperialBMI()