4

我一直在为我的 java 类做一个项目。对于该项目,我必须让用户输入输入并计算他们的体重指数和体表面积,该程序应该保持运行,直到用户输入“q”。我无法让我的程序在放入“q”时停止运行,它只会崩溃。此外,我对 Java 和一般编程非常陌生,因此我将不胜感激。我的代码如下。谢谢 : )

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
        String weight;                     // Weight in kilograms
        String height;                     // Height in meters
        String classification;             // Classifies the user into BMI categories 
        double bsa;                        // Body surface area



        do {
            System.out.print("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms.");
            weight = stdIn.next();
            System.out.print("Enter height in meters: ");
            height = stdIn.next();
            double height2 = Double.parseDouble(height);
            double weight2 = Double.parseDouble(weight);
            bmi = weight2/(height2*height2);
        if (bmi < 18.5)
        {
            classification = "Underweight";
        }
        else if (bmi < 25)
        {
            classification = "Normal";
        }
        else if (bmi < 30)
        {
            classification = "Overweight";
        }
        else
        {
            classification = "Obese";
        }

        System.out.println("Your classification is: " + classification);
        bsa = Math.sqrt(((height2*METERS_TO_CM)*weight2)/BSA_CONSTANT);
        System.out.printf("BMI: %.1f\n", bmi);
        System.out.printf("BSA: %.2f\n", bsa);


        System.out.println("Hit 'q' to quit");
        } while (stdIn.nextLine().compareToIgnoreCase("q")==0);






    }
}
4

5 回答 5

1

我猜你的“q”输入是写的weight,因此你尝试将它解析为一个 Double,它会抛出一个未处理的异常并停止执行。

您应该处理此异常并让系统在触发它时中断 while 循环。

于 2011-02-24T23:20:51.317 回答
1

您正在为您的 while 循环条件抓取整行。

尝试抓住next()而不是nextLine().

此外,您正在查看它确实等于 0 ... 表示相等。我会改为!=改为。您想在下一个标记不是 Q 时继续循环。

于 2011-02-24T23:21:58.910 回答
0

让我们进行结构上的更改,以使您更容易做到这一点。

我们将对其进行更改,以便您的 do-while 循环始终运行,直到您明确告诉它停止。

您当前的时间是:

while(stdIn.nextLine().compareToIgnoreCase("q")==0);

哪个工作正常,但我们有一个更简单的方法来做到这一点。你听说过这个break声明吗?

我建议你使用break。该语句将“打破”您的 while 循环;基本上它告诉程序在被调用时停止循环。这将比您有些混乱的do-while 更容易理解。

do {

   //Your Do code goes here, as before
   ...
   //

   //Your newly added break statement will go here. 
   //This breaks out of the while loop when your inputed 'choice' value is
   //equal to the string of "q" (for quit)
   if (Choice.equals("q"))){
     break;
     //When break is called nothing else in the loop will run
   }

   //Same thing but with the string of "quit"
   if (Choice.equals("quit"){
     break;
   }



}while (true); 
//Your new while statement is simply while(true) which will run until break is called

希望这对你有帮助。

于 2011-02-27T20:28:55.100 回答
0

实际上不要使用循环。由于通过回答太多问题来最大化调用堆栈是不切实际的,因此只需将整个操作设为函数即可。在函数结束时,如果结果不是 Q,则调用自身。

于 2013-02-20T21:37:19.957 回答
0

它的简单使用

while (true)
{
    Console.WriteLine("Start processing");
    //processing code here
    Console.WriteLine("finish processing");

    Console.WriteLine("start again? y/n?");
    if (Console.ReadLine().Equals("n"))
        break;
}
于 2016-06-23T07:32:43.810 回答