0
我剛開始學習java 2個月前,我有一個項目可以創建一個可以使用英制系統和公制系統的BMI計算器。 我創造了它,但是我得到了Nan作爲BMI的答案。我沒有在任何語法錯誤,我不明白爲什麼我收到NaN`在我的java項目上獲得NaN
import java.util.Scanner;
import java.util.InputMismatchException;
public class BmiCalculator
{
Scanner input = new Scanner(System.in);
// ConvertSystem convert = new ConvertSystem();
String name;
double height;
double stone;
double pound;
double feet;
double inch;
double kg;
double meters;
double wheight;
double BMI;
int MetricOrImperial ;
public void getIntro()
{
System.out.println("Welcome to the Body Mass Index Calculator");
System.out.println("Please enter your name:");
name = input.nextLine();
System.out.printf("Hello %s !\n", name);
}
public void metricOrImperial()
{ boolean continueLoop = true;
do
{
try
{
System.out.println("Please choose which measurement system you want to use; \n ");
System.out.println("For Imperial type number 1:\nFor Metric type number 2 \n");
MetricOrImperial = input.nextInt();
continueLoop = false;
}// end try
catch (InputMismatchException inputMismatchException)
{
System.err.printf("\nException: %s\n",inputMismatchException);
input.nextLine();
System.out.println
("\nYou must enter number 1 or 2\n Please try again");
}// end catch
}// end do
while (continueLoop);
} // end imperialOrMetric
public void getImperial()
{ boolean continueLoop = true;
do{
try{
if (MetricOrImperial == 1)
{
System.out.println("Please enter your wheight in Stones: ");
stone = input.nextDouble();
System.out.println("Enter your wheight in Pounds: ");
pound = input.nextDouble();
System.out.println("Please enter your height in feets: ");
feet = input.nextDouble();
System.out.println("Enter your height in inch: ");
inch = input.nextDouble();
}
continueLoop= false;
} // end try
catch (InputMismatchException inputMismatchException)
{
System.err.printf("\nException: %s\n",inputMismatchException);
input.nextLine();
System.out.println
("\nPlease enter only number\n Try again!");
}// end catch
}while(continueLoop);
}
public void getMetric()
{ boolean continueLoop = true;
do{
try{
if (MetricOrImperial == 2)
{
System.out.println("Please enter your wheight in Kg ");
kg = input.nextDouble();
System.out.println("Please enter your height in Meters: ");
meters = input.nextDouble();
}
continueLoop= false;
} // end try
catch (InputMismatchException inputMismatchException)
{
System.err.printf("\nException: %s\n",inputMismatchException);
input.nextLine();
System.out.println
("\nPlease enter only number\n Try again!");
}// end catch
}while(continueLoop);
}// end getMetric
public void convertToMetric()
{
if (MetricOrImperial == 1)
{
wheight = (stone * 6.3502) + (pound * 0.4536);
height = (feet * 0.3048) + (inch * 0.0254);
}
else
{
wheight = kg;
height = meters;
}
}
public void getBmi()
{
BMI = wheight/(height* height) ;
System.out.println(BMI);
}
} // endBMI calculator
`
import java.util.*;
public class BmiCalculatorTest extends BmiCalculator {
public static void main(String[] args)
{
BmiCalculator bmi = new BmiCalculator();
bmi.getIntro();
bmi.metricOrImperial();
bmi.getImperial();
bmi.getMetric();
bmi.getBmi();
我敢打賭你的避風港」 t _actually_初始化高度。 –
您可以添加您的主要方法或顯示您調用方法的順序和您輸入的輸入。 – develop1
你不要調用'bmi.convertToMetric();'。 (它是_weight_!) –