开发者

While loop help

I have a project for class where I have to ask the user for input for a body mass index and body surface area calculator. I have an if else statement that I nee开发者_StackOverflowd to put in a while-loop. I need to have it go through and if the user enters a 'w' bring up the weight variable and a 'h' for height. Also if the user enters a 'q' to quit the program. I need help on how to create the while loop.

import java.util.*;
public class Assignment2 {


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
    char quit = stdIn.nextLine().charAt(0);

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

    while (quit != 'q');
    {
        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(((height*METERS_TO_CM)*weight)/BSA_CONSTANT);
        System.out.printf("BMI: %.1f\n", bmi);
        System.out.printf("BSA: %.2f\n", bsa);
    }
}
}


Here is the basic logic.

   String inp = ask the user for input;
   while(!inp.equalsIgnoreCase("q")){
       if(inp is w){

       }else{
          if(inp is h){

          }
       }
       inp = ask user for another input;
   }


I assume that you have variables outside of this loop so that you can access them anywhere. So you would have something like this:

while(true) // so your loop will continue forever

here prompt the user for one of three choices that you gave above, then put in a temporary variable. For example,

scanner sc=new Scanner(params)

choice=s.getnext() //or the equivalent method

prompt for the value

use if statements to see what variable to modify, based on char value of "choice" above

if the user inputs a q, use the return or break statements to break out of your while loop. Otherwise, I'm sure you can figure out the rest.


Well, your while loop is correct, so your question may not actually be how to create the while loop but how to make your loop do the logic you intend.

While you can simply prompt before the loop and at the end of every loop, that only works for simple problems. Since you are handling two inputs at once and have if statements running around, I would recommend using a flag (a boolean that controls whether to keep looping)

boolean keepLooping = true;

while(keepLooping)
{
    String userInput = //get input from user here

    if(userInput.equals(//whatever))
    {
        //do something
    }
    else if(userInput.equals(//whatever else))
    {
        //more stuff
    }
    else if(userInput.equals("q")) //the quitting condition!
    {
        keepLooping = false; //this tells our program to stop looping
    }
}
//we reached the end of the loop and keepLooping == false, so program will exit

and in the other if/else blocks, you can do all the BMI stuff you want.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜