2016-12-14 2 views
0

Ich habe gerade angefangen, Java vor 2 Monaten zu lernen, und ich habe ein Projekt, um einen BMI-Rechner zu erstellen, der das imperiale System und das metrische System verwenden kann. Ich habe es erstellt, aber ich bekomme Nan als Antwort für BMI. Ich habe keine Syntaxfehler drin, und ich verstehe nicht, warum ich NaN`bekommen NaN auf meinem Java-Projekt

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

Dividieren Ich würde Sie haven wetten‘ t _actually_ initialisierte Höhe. –

+1

Können Sie Ihre Hauptmethode hinzufügen oder zeigen, welche Reihenfolge Ihr Aufruf die Methode und die Eingabe Ihrer Eingabe. – develop1

+1

Sie rufen 'bmi.convertToMetric();' nicht auf. (Und es ist _weight_!) –

Antwort

0

Der Grund ist es nicht immer bin ist Arbeiten ist, dass Sie niemals bmi.convertToMetric() aufgerufen haben, um die Höhe zu berechnen.

Wenn Sie diesen Code Ihren Code ausführen arbeitet

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(); 
} 

Der Grund, warum Sie NaN bekam, dass Höhe wurde auf 0,0, so versucht man 0.0/0.0 in getBmi()