开发者

ending a do-while loop

开发者 https://www.devze.com 2023-02-13 21:49 出处:网络
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.

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: " + weig开发者_如何学Pythonht + " 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 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.

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


The problem is these lines:

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

Whenever the response is 'q', then each of these tests is satisfied. Get rid of them; they do nothing you want. Instead, put a plain 'else' after the last valid char test and print the "not a valid choice" prompt.


You have to have something like,

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

 }


try

else if (response.charAt(0).contains('q'))

0

精彩评论

暂无评论...
验证码 换一张
取 消