按'q'结束for循环

时间:2011-02-24 23:13:53

标签: java do-while

我一直在为我的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);






    }
}

5 个答案:

答案 0 :(得分:1)

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

你应该处理这个异常并让系统在触发时打破while循环。

答案 1 :(得分:1)

你正在为你的while循环条件抓住整行。

尝试抓住next()代替nextLine()

另外,你看着它等于0 ......意思相等。我会将其更改为!=。您希望在下一个令牌不是Q时继续循环。

答案 2 :(得分:0)

让我们进行结构性改变,让您更容易做到。

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

你现在的时间是:

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

哪个有效,但我们有一个更简单的方法来做到这一点。你听说过break陈述吗?

我建议您使用break。这句话将“打破”你的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

希望这对你有所帮助。

答案 3 :(得分:0)

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

答案 4 :(得分: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;
}
相关问题