【问题标题】:While loop helpWhile 循环帮助
【发布时间】:2011-02-24 11:07:26
【问题描述】:

我有一个课程项目,我必须要求用户输入体重指数和体表面积计算器。我有一个 if else 语句,我需要将其放入一个 while 循环中。我需要让它通过,如果用户输入“w”,则显示重量变量和“h”表示高度。此外,如果用户输入“q”以退出程序。我需要有关如何创建 while 循环的帮助。

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

【问题讨论】:

  • 您忘记再次发布完整代码。 create a new question值得吗?我不知道。我建议阅读更多文档。

标签: java while-loop


【解决方案1】:

这是基本逻辑。

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

【讨论】:

    【解决方案2】:

    我假设您在此循环之外有变量,以便您可以在任何地方访问它们。所以你会有这样的东西:

    while(true) // 这样你的循环就会一直持续下去

    这里提示用户选择您上面给出的三个选项之一,然后放入一个临时变量。例如,

    scanner sc=new Scanner(params)

    choice=s.getnext() //或等效方法

    提示输入值

    使用 if 语句查看要修改的变量,基于上面 "choice" 的 char 值

    如果用户输入 q,请使用 returnbreak 语句跳出您的 while 循环。否则,我相信您可以解决剩下的问题。

    【讨论】:

      【解决方案3】:

      嗯,你的 while 循环是正确的,所以你的问题实际上可能不是如何创建 while 循环,而是如何让你的循环执行你想要的逻辑。

      虽然您可以在循环之前和每个循环结束时简单地提示,但这仅适用于简单的问题。由于您一次处理两个输入并且有 if 语句运行,我建议使用一个标志(一个控制是否继续循环的布尔值)

      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
      

      在其他 if/else 块中,您可以做所有您想要的 BMI 操作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-24
        • 1970-01-01
        • 2018-06-20
        • 2010-11-29
        • 2011-05-14
        • 2010-11-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多