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 循环连续运行while( true ) { ... }并检查输入值(当您要求 H 或 W 时)检查它是否为 q。如果是,则让程序退出。

于 2011-02-27T03:27:25.283 回答
0

什么for循环?- 没关系,因为它已被纠正

我确实看到您的 Scanner 对象存在潜在问题,但是因为它不处理行尾标记,您可能需要调用 stdIn.nextLine(); 每次调用 stdIn.next() 之后;或 stdIn.nextDouble();,这样您就可以正确处理行尾字符。

于 2011-02-27T03:26:16.833 回答
0

stdIn.next()在太多地方调用:在循环开始时,在循环终止测试中,以及在最后两个分支中(顺便说一句,它们都应该被单个 替换else { ... })。(顺便说一句,循环是一个 do 循环,而不是一个 for 循环。)

另一个问题是使用 Scanner 的输入和使用 System.out 的输出的混合。我建议在尝试通过扫描仪进行任何输入之前调用 System.out.flush()。

于 2011-02-27T03:32:38.083 回答