在我的java项目

时间:2016-12-14 00:13:39

标签: java

我刚开始学习java 2个月前,我有一个项目来创建一个可以使用英制系统和公制系统的BMI计算器。 我创建了它,但我得到了Nan作为BMI的答案。我没有任何语法错误,我也不明白为什么我会收到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();
        
        




1 个答案:

答案 0 :(得分:0)

它无效的原因是你从未打电话给bmi.convertToMetric()来计算身高。

如果您运行此代码,则代码可以正常运行

public static void main(String[] args)
{
   BmiCalculator bmi = new BmiCalculator();
    bmi.getIntro();
    bmi.metricOrImperial();
    bmi.getImperial();
    bmi.getMetric();
    bmi.convertToMetric(); //Added call to converToMetric to calculate height
    bmi.getBmi();
}

你得到NaN的原因是高度被设置为0.0所以你试着在getBmi()中划分0.0 / 0.0