Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java While循环帮助_Java_While Loop - Fatal编程技术网

Java While循环帮助

Java While循环帮助,java,while-loop,Java,While Loop,我有一个课程项目,我必须要求用户输入体重指数和体表面积计算器。我有一个if-else语句需要放入while循环中。我需要让它通过,如果用户输入一个“w”,则调出重量变量和一个“h”表示高度。此外,如果用户输入“q”退出程序。我需要关于如何创建while循环的帮助 import java.util.*; public class Assignment2 { public static void main(String[] args) { //Scanner Scanner s

我有一个课程项目,我必须要求用户输入体重指数和体表面积计算器。我有一个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);
    }
}
}
import java.util.*;
公共课堂作业2{
公共静态void main(字符串[]args){
//扫描器
扫描仪标准输入=新扫描仪(System.in);
//变数
final double METERS_TO_CM=100;//将米转换为厘米的常数
最终双BSA_常数=3600;//BSA除以的常数
双倍体重指数;//体重指数
双倍重量;//重量单位为千克
双倍高度;//以米为单位的高度
字符串分类;//将用户分类为BMI类别
双bsa;//体表面积
char quit=stdIn.nextLine().charAt(0);
System.out.print(“欢迎使用BMI和BSA计算器开始以千克为单位输入体重”);
重量=stdIn.nextDouble();
系统输出打印(“以米为单位输入高度:”;
高度=stdIn.nextDouble();
体重指数=体重/(身高*身高);
while(退出!=“q”);
{
如果(体重指数<18.5)
{
分类=“体重不足”;
}
否则,如果(体重指数<25)
{
分类=“正常”;
}
否则,如果(体重指数<30)
{
分类=“超重”;
}
其他的
{
分类=“肥胖”;
}
System.out.println(“您的分类为:“+分类”);
bsa=数学sqrt((高度*米到厘米)*重量)/bsa_常数);
System.out.printf(“BMI:%.1f\n”,BMI);
System.out.printf(“BSA:%.2f\n”,BSA);
}
}
}

以下是基本逻辑

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

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

while(true)//因此您的循环将永远继续

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

scanner sc=新扫描仪(参数)

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

提示输入值

使用if语句根据上面“choice”的char值查看要修改的变量


如果用户输入q,请使用
return
break
语句中断while循环。否则,我相信您可以解决其余问题。

嗯,您的while循环是正确的,因此您的问题可能不是如何创建while循环,而是如何使您的循环执行您想要的逻辑

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


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

您忘了再次发布完整的代码。值得一试吗?我不知道。我建议多读一些文档。
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