2011-02-27 181 views
0

大家好,我真的很接近完成这一点,但我有我的for循环的问题。我需要我的程序有一个用户输入一个“W”或“H”,让他们进入一个新的体重或身高。当他们做我的循环似乎由于某种原因停止。我do-while循环不循环

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); 
} 
} 
+0

我的意思是做while while循环对不起 – Brad 2011-02-27 03:24:22

+0

你有没有想过可能使用Switch Case而不是使用Else IF,这意味着你可以使用While循环来继续循环,直到你设置一个布尔值,当Case命中'Q ”。只是一个想法。 – 2011-02-27 03:30:35

回答

0

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

我确实看到了您的扫描仪对象的潜在问题,但因为它不处理行结束符号,并且您可能想要调用stdIn.nextLine();在每次调用stdIn.next()之后;或stdIn.nextDouble();,只是这样你才能正确处理行尾字符。

3

这样做stdIn.next().compareToIgnoreCase("q")!=0你所要求的计算机绘制的下一个值从标准输入缓冲区。这意味着,你做你的处理后,while循环等待数据从标准输入进来它决定继续处理之前。你会具有while(true) { ... } while循环连续运行,并检查输入值(当你问的H或W)进行检查,看它是否是一个Q的更好。如果是,则退出程序。

0

你打电话的地方太多stdIn.next():在循环的开始,在循环终止测试,并在最后两个分支(其中,顺便说一句,都应该由一个else { ... }代替)。 (环路是一个do循环,顺便说一下,不for循环。)

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