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番目の 'println'にあります。あなたは 'heightInInches()'と 'wightInPounds()'をもう一度呼びます。最初の呼び出しで戻り値を保存して、 'outputBMI()'メソッドに送信する必要があります。 – rafid059
入力を一度要求するだけではありません。 'heightInInches()'を複数回呼び出すと、呼び出すたびに入力が求められます。 – nhouser9
@RafiduzzamanSonnetあなたは何を意味するのか混乱していますか?それが私の目的を助けるなら、インチで高さを呼んで、それを表示することです。体重をポンドで呼んで表示します。次に、BMIを計算して表示します。 – Josh