2011-02-24 175 views
4

我一直在为我的java类做一个项目。对于该项目,我必须让用户输入输入并计算他们的体重指数和体表面积,程序应该保持运行直到用户输入“q”。当“q”被放入时,我无法让程序停止运行,只是崩溃。另外我对Java和编程一般都很新,所以我会很感激任何帮助。我的代码如下。 感谢:)按'q'结束for循环

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); 






    } 
} 
+0

这些天,谁家将有一个关于计算BMI的问题,它不会做作业。 :-) – corsiKa 2011-02-24 23:17:21

+0

是啊,现在我只是担心功课:) – Brad 2011-02-24 23:19:23

+0

哦,这不是什么反对你,或问题。这是每个人在一个班级或另一个班级中做的“经典”作业作业之一。我只知道其中一天,一些对编程一无所知的保险人将会试图为他的保险报告制作BMI计算器,并且人们会认为它是功课。呵呵呵。 – corsiKa 2011-02-24 23:26:14

回答

1

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

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

+0

我编译他的代码 - 他没有得到一个例外。虽然好想。 – corsiKa 2011-02-24 23:22:21

+0

@ glowcoder其实我不知道我的理解 - 程序应该停止运行,但相反,它崩溃没有例外?有什么不同 ? – Dunaril 2011-02-24 23:24:24

+0

那么,它不会“崩溃”。编译他的代码,这是有道理的。它不会在循环结尾查询用户输入。 – corsiKa 2011-02-24 23:25:09

1

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

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

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

+0

是的!工作感谢你我一直坐在这里看着最后2个小时试图弄清楚这一点。我很感激 – Brad 2011-02-24 23:27:42

+1

我知道它的工作原理,因为我将它编译到我的机器上的代码中。 :-)现在,这些都是你所问问题的答案......但是...... Dunaril,虽然他没有回答这个问题,但提出了一个非常重要的观点。当你把体重放在“abc”时会发生什么?它死了......你实际上可以从中恢复!我不知道你是否了解异常处理,但如果你没有,你应该看看它。 – corsiKa 2011-02-24 23:29:52

+0

是的,我知道我会尝试做这件事后,我得到了这个工作。 – Brad 2011-02-24 23:33:11

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 

希望这对您有所帮助。

0

实际上不使用循环。由于回答太多问题会让某些人无法将呼叫堆栈放大,因此只需将整个操作作为一项功能即可。在函数结束时,调用本身如果结果不是Q.

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; 
}