0

みなさん、こんにちは。これで完成に近づいていますが、for ループに問題があります。ユーザーに「w」または「h」を入力させて、新しい体重または高さを入力させるプログラムが必要です。彼らが行うと、何らかの理由でループが停止するようです。

package Assignments;
import java.util.*;
public class assignment3 {


public static void main(String[] args) {

    //Scanner
    Scanner stdIn = new Scanner(System.in);

    //Variables
    final double METERS_TO_CM = 100;   // The constant to convert meters to centimeters
    final double BSA_CONSTANT = 3600;  // The constant to divide by for bsa
    double bmi;                        // Body Mass Index
    double weight;                     // Weight in kilograms
    double height;                     // Height in meters
    String classification;             // Classifies the user into BMI categories 
    double bsa;                        // Body surface area



    System.out.print("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms.");
    weight = stdIn.nextDouble();
    System.out.print("Enter height in meters: ");
    height = stdIn.nextDouble();
    bmi = weight/(height*height);
    bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);


    if (bmi < 18.5)
    {
        classification = "Underweight";
    }
    else if (bmi < 25)
    {
        classification = "Normal";
    }
    else if (bmi < 30)
    {
        classification = "Overweight";
    }
    else
    {
        classification = "Obese";
    }
    System.out.println("Choose Options below to set height and weight");
    System.out.println("Your classification is: " + classification);
    System.out.println("(H)eight: " + height + " meters");
    System.out.println("(W)eight: " + weight + " kilograms");
    System.out.printf("BMI: %.1f\n", bmi);
    System.out.printf("BSA: %.2f\n", bsa);
    System.out.println("(Q)uit");

    do {


        String response = stdIn.next();

        if (response.charAt(0)== 'w') 
        {
            System.out.println("Enter new weight: ");
            weight = stdIn.nextDouble();
            System.out.println("Choose Options below to set height and weight");
            System.out.println("Your classification is: " + classification);
            System.out.println("(H)eight: " + height + " meters");
            System.out.println("(W)eight: " + weight + " kilograms");
            System.out.printf("BMI: %.1f\n", bmi);
            System.out.printf("BSA: %.2f\n", bsa);
            System.out.println("(Q)uit");
        }
        else if (response.charAt(0) == 'h')
        {
            System.out.println("Enter new height: ");
            height = stdIn.nextDouble();
            System.out.println("Choose Options below to set height and weight");
            System.out.println("Your classification is: " + classification);
            System.out.println("(H)eight: " + height + " meters");
            System.out.println("(W)eight: " + weight + " kilograms");
            System.out.printf("BMI: %.1f\n", bmi);
            System.out.printf("BSA: %.2f\n", bsa);
            System.out.println("(Q)uit");
        }
        else if (response.charAt(0)!= 'w')
        {
            System.out.println("That is not a valid choice try again");
            response = stdIn.next();
        }
        else if (response.charAt(0)!= 'h')
        {
            System.out.println("that is not a valid choise try again");
            response = stdIn.next();
        }
    } while (stdIn.next().compareToIgnoreCase("q")!=0);
}
}
4

3 に答える 3

3

そうstdIn.next().compareToIgnoreCase("q")!=0することで、コンピューターに STDIN バッファーから次の値を引き出すように要求しています。これは、処理が完了した後、While ループが処理を続行することを決定する前に、STDIN からデータが来るのを待っていることを意味します。while( true ) { ... }while ループを継続的に実行し、入力値をチェックして ( H または W を要求するとき)、それが q かどうかを確認する方がよいでしょう。その場合は、プログラムを終了します。

于 2011-02-27T03:27:25.283 に答える
0

あまりにも多くの場所で呼び出していますstdIn.next(): ループの開始時、ループ終了テスト、および最後の 2 つの分岐 (ちなみに、どちらも単一の に置き換える必要がありますelse { ... })。(ちなみに、ループは for ループではなく do ループです。)

もう 1 つの問題は、Scanner を使用した入力と System.out を使用した出力が混在していることです。Scanner からの入力を試みる前に System.out.flush() を呼び出すことをお勧めします。

于 2011-02-27T03:32:38.083 に答える
0

ループは?-- 修正済みなので気にしないでください

Scanner オブジェクトには潜在的な問題がありますが、行末トークンを処理しないため、stdIn.nextLine(); への呼び出しが必要になる場合があります。stdIn.next() を呼び出すたびに; または stdIn.nextDouble(); で、行末文字を適切に処理できるようにします。

于 2011-02-27T03:26:16.833 に答える