首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >结束do-while循环

结束do-while循环
EN

Stack Overflow用户
提问于 2011-02-27 12:49:04
回答 4查看 1.4K关注 0票数 0

好的,我有一个do-while循环,它应该在use点击'q‘时结束,但它给了我错误消息,请帮助我。

代码语言:javascript
复制
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");
    }
}
EN

回答 4

Stack Overflow用户

发布于 2011-02-27 13:00:11

q不等于wh。因此,条件对于else if (response.charAt(0)!= 'w')为真,因此您不会使用实际的else if (response.charAt(0) == 'q')条件来破坏。

所以,把你的最后3个else if放在这里-

代码语言:javascript
复制
    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();
    }
票数 3
EN

Stack Overflow用户

发布于 2011-02-27 12:59:52

问题是下面这几行:

代码语言:javascript
复制
else if (response.charAt(0)!= 'w')
// ...
else if (response.charAt(0)!= 'h')

每当响应是'q‘时,这些测试中的每一个都是满足的。摆脱它们;它们不会做任何你想要的事情。取而代之的是,在最后一个有效的字符测试之后添加一个简单的'else‘,并打印"not a valid choice“提示。

票数 2
EN

Stack Overflow用户

发布于 2011-02-27 12:57:55

你必须有像这样的东西,

代码语言:javascript
复制
 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();

 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5131521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档