结束do-while循环 [英] ending a do-while loop

查看:300
本文介绍了结束do-while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有一个do-while循环,该循环应该在使用次数达到 q时结束,但它给了我错误消息,请帮忙。

Ok I have a do-while loop that's supposed to end when the use hits 'q' but it is giving me the error message instead please help.

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


推荐答案

q 不等于 w h 。因此,如果(response.charAt(0)!='w'),则 else的条件成立,因此您不会实际使用 else如果(response.charAt(0)=='q')条件中断。

q is not equal to either w or h. So, condition is true for else if (response.charAt(0)!= 'w') and so you aren't going to actual else if (response.charAt(0) == 'q') condition to break.

因此,将最后3个 else if 以这种方式-

So, place your last 3 else if in this fashion -

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

这篇关于结束do-while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆