2016-11-01 2 views
0

ここで、この質問が先に聞かれたかもしれませんが、見つかりませんでした。だから私は謝罪する。Javaプログラムがプロンプトを表示せずに2回ループする

何らかの理由で、コードが2回実行されているように見えますが、コードを一度だけ入力するだけです。誰かが私のミスに私を導くことができますか?

ありがとうございました。

import java.util.Scanner; 

public class BMI 
{ 

    static Scanner kybd = new Scanner(System.in); 

public static void main(String[] args) 
{ 
    System.out.println("Height in inches: " + heightInInches()); 
    System.out.println("Weight in pounds: " + weightInPounds()); 
    System.out.println(outputBMI(heightInInches(),weightInPounds())); 
} 

public static int heightInInches() 
{ 

    System.out.print("Enter your height in feet: "); 
    int feet = kybd.nextInt(); 

    //feet validation 
    while(feet < 2 || feet > 7) 
    { 
     System.out.println("Input not vaild."); 

     System.out.print("Enter your height in feet: "); 
     feet = kybd.nextInt(); 
    } 

    System.out.print("Enter your height in inches: "); 
    int inches = kybd.nextInt(); 

    //inches validation 
    while(inches < 0 || inches > 13){ 
     System.out.println("Input not vaild."); 
     System.out.print("Enter your height in inches: "); 
     inches = kybd.nextInt(); 
    } 

    int totalInches = inches + (feet * 12); 

    return totalInches; 

} 

public static int weightInPounds() 
{ 
    System.out.print("Enter your weight in stone: "); 
    int stone = kybd.nextInt(); 

    //stone validation 
    while(stone < 3 || stone > 30) 
    { 
     System.out.println("Input invalid."); 
     System.out.print("Enter your height in stone: "); 
     stone = kybd.nextInt(); 
    } 

    System.out.print("Please enter your weight in pounds: "); 
    int pounds = kybd.nextInt(); 

    //pounds validation 
    while(pounds < 0 || pounds > 30) 
    { 
     System.out.println("Input invalid."); 
     System.out.print("Please enter your weight in pounds: "); 
     pounds = kybd.nextInt(); 
    } 

    int totalPounds = pounds + (stone * 14); 

    return totalPounds; 
} 

public static double outputBMI(double height, double weight) 
{ 
    double BMI = (weight * 703)/(height/height); 

    return BMI; 
} 

}

+3

メインメソッドの3番目の 'println'にあります。あなたは 'heightInInches()'と 'wightInPounds()'をもう一度呼びます。最初の呼び出しで戻り値を保存して、 'outputBMI()'メソッドに送信する必要があります。 – rafid059

+0

入力を一度要求するだけではありません。 'heightInInches()'を複数回呼び出すと、呼び出すたびに入力が求められます。 – nhouser9

+0

@RafiduzzamanSonnetあなたは何を意味するのか混乱していますか?それが私の目的を助けるなら、インチで高さを呼んで、それを表示することです。体重をポンドで呼んで表示します。次に、BMIを計算して表示します。 – Josh

答えて

0

あなたが必要な場所に、あなたはこれらの変数を再利用し、一度だけあなたのheightInInchesweightInPoundsを呼び出して、あなたの主な方法でいくつかのローカル変数に値を格納する必要があるコメントでmentionnedとして多くの。たとえば、メインメソッドを次のように更新することができます。

public static void main(String[] args) 
{ 
    int height = heightInInches(); 
    int weight = weightInPounds(); 
    System.out.println("Height in inches: " + height); 
    System.out.println("Weight in pounds: " + weight); 
    System.out.println(outputBMI(height,weight)); 
} 
関連する問題