0

好的,我有一个 do-while 循环,它应该在使用命中“q”时结束,但它给了我错误消息,请帮忙。

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);       // Calculates BMI
        bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);    // Calculates BSA


        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");
        String response = stdIn.next();

        do {

            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");
                bmi = weight/(height*height);       
                bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);
                response = stdIn.next();
            }
            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");
                bmi = weight/(height*height);       
                bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);
                response = stdIn.next();
            }
            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();
            }
            else if (response.charAt(0) == 'q')
            {
                break;
            }
        } while (response != "q");
    }
}
4

4 回答 4

3

q不等于wh。所以,条件是正确的else if (response.charAt(0)!= 'w'),所以你不会去else if (response.charAt(0) == 'q')打破实际条件。

else if所以,以这种方式放置你的最后 3 个-

    else if (response.charAt(0) == 'q')
    {
            break;
    }
    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();
    }
于 2011-02-27T05:00:10.960 回答
2

问题是这些行:

else if (response.charAt(0)!= 'w')
// ...
else if (response.charAt(0)!= 'h')

只要响应为“q”,则满足这些测试中的每一个。摆脱它们;他们什么都不做。相反,在最后一个有效的字符测试之后放置一个简单的“else”并打印“不是一个有效的选择”提示。

于 2011-02-27T04:59:52.007 回答
1

你必须有类似的东西,

 if (response.charAt(0)== 'w') 
 {
   ...
 }
 else if(response.charAt(0)== 'h')
 {
   ...
 }
 else if(response.charAt(0)== 'q')
 {
   System.exit(0);
 }
 else
 {
      System.out.println("That is not a valid choice try again");
                response = stdIn.next();

 }
于 2011-02-27T04:57:55.110 回答
0

尝试

否则 if (response.charAt(0).contains('q'))

于 2011-02-27T04:55:34.183 回答