End the for loop by pressing 'q'

I am doing a project for my java class. For the project, I need the user to enter input and calculate the body mass index and body surface area, the program must remain on until the user enters "q". I can not make my program stop working when the "q" is inserted into it just crashing. I am also very new to java and programming in general, so I would appreciate any help. My code is as follows. Thanks:)

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 String weight; // Weight in kilograms String height; // Height in meters String classification; // Classifies the user into BMI categories double bsa; // Body surface area do { System.out.print("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms."); weight = stdIn.next(); System.out.print("Enter height in meters: "); height = stdIn.next(); double height2 = Double.parseDouble(height); double weight2 = Double.parseDouble(weight); bmi = weight2/(height2*height2); if (bmi < 18.5) { classification = "Underweight"; } else if (bmi < 25) { classification = "Normal"; } else if (bmi < 30) { classification = "Overweight"; } else { classification = "Obese"; } System.out.println("Your classification is: " + classification); bsa = Math.sqrt(((height2*METERS_TO_CM)*weight2)/BSA_CONSTANT); System.out.printf("BMI: %.1f\n", bmi); System.out.printf("BSA: %.2f\n", bsa); System.out.println("Hit 'q' to quit"); } while (stdIn.nextLine().compareToIgnoreCase("q")==0); } } 
+4
source share
5 answers

I would suggest that your "q" input is written in weight , and so you are trying to parse it into Double, which throws an unhandled Exception and stops execution.

You must handle this exception and make the system interrupt the while loop when it starts.

+1
source

You grab the entire row for your while loop state.

Try just grab next() instead of nextLine() .

In addition, you look while it is 0 ... means equal. I would change that to != . You want to continue the cycle until the next token is NOT Q.

+1
source

Make structural changes to make it easier for you.

We are going to change it so that the do-while loop always works until you explicitly stop it.

Current time:

while(stdIn.nextLine().compareToIgnoreCase("q")==0);

Which works fine, but we have an easier way to do this. Have you heard of the break statement?

I suggest you use break . This statement will break you out of the while loop; it basically tells the program to end the loop when it is called. It will be a little easier to follow than your somewhat confusing business.

 do { //Your Do code goes here, as before ... // //Your newly added break statement will go here. //This breaks out of the while loop when your inputed 'choice' value is //equal to the string of "q" (for quit) if (Choice.equals("q"))){ break; //When break is called nothing else in the loop will run } //Same thing but with the string of "quit" if (Choice.equals("quit"){ break; } }while (true); //Your new while statement is simply while(true) which will run until break is called 

Hope this is helpful for you.

0
source

Do not actually use a loop. Since it is impractical that someone will ever increase the number of calls by answering too many questions, just make the whole operation a function. At the end of the function, call yourself if the result is not Q.

0
source

its simple use

 while (true) { Console.WriteLine("Start processing"); //processing code here Console.WriteLine("finish processing"); Console.WriteLine("start again? y/n?"); if (Console.ReadLine().Equals("n")) break; } 
0
source

Source: https://habr.com/ru/post/1341233/


All Articles