简体   繁体   English

我的do-while循环没有循环

[英]My do-while loop isn't looping

Hello everybody I'm really close to finishing this but I'm having problems with my for loop. 大家好,我真的很接近完成此工作,但是我的for循环遇到了问题。 I need my program to have a user enter either a 'w' or 'h' to have them enter a new weight or height. 我需要让用户输入“ w”或“ h”来让他们输入新的体重或身高。 When they do my loop seems to stop for some reason. 当他们这样做时,我的循环似乎由于某种原因而停止了。

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

By doing stdIn.next().compareToIgnoreCase("q")!=0 you are asking the computer to draw the next value out of the STDIN buffer. 通过执行stdIn.next().compareToIgnoreCase("q")!=0您要求计算机从STDIN缓冲区中提取下一个值。 This means that after you've done your processing the While loop is waiting for data to come in from STDIN before it decides to continue processing. 这意味着在完成处理后,While循环在决定继续处理之前正在等待来自STDIN的数据输入。 You would be better of having the while loop run continuously with while( true ) { ... } and checking the input value ( when you ask for the H or W ) be check to see if it's a q. 您最好使while循环与while( true ) { ... }连续运行,并检查输入值(当您要求输入H或W时),以检查其是否为q。 When it is, then have the program exit. 如果是这样,则使程序退出。

What for loop? 什么循环? -- never mind as it's been corrected -没关系,因为它已得到纠正

I do see potential problems with your Scanner object however as it doesn't deal with end of line tokens and you might want to place a call to stdIn.nextLine(); 我确实看到了您的Scanner对象的潜在问题,因为它不处理行尾标记,并且您可能想调用stdIn.nextLine();。 after every call to stdIn.next(); 在每次调用stdIn.next()之后; or stdIn.nextDouble();, just so you can properly handle the end of line character. 或stdIn.nextDouble();,这样您就可以正确处理行尾字符。

You're calling stdIn.next() in too many places: at the start of the loop, in the loop termination test, and in the last two branches (which, by the way, should both be replaced by a single else { ... } ). 您在太多地方调用了stdIn.next() :在循环开始时,在循环终止测试中以及在最后两个分支中(顺便说一下,这两个分支都应被一个单独的else { ... }代替) else { ... } )。 (The loop is a do-loop, by the way, not a for-loop.) (顺便说一下,循环是一个do循环,而不是for循环。)

Another problem is the mixing of input using a Scanner and output using System.out. 另一个问题是使用Scanner混合输入和使用System.out输出混合。 I suggest calling System.out.flush() before attempting any input through the Scanner. 我建议在尝试通过Scanner进行任何输入之前先调用System.out.flush()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM