Java 结束do-while循环
好的,我有一个do-while循环,应该在使用点击'q'时结束,但是它给了我错误消息,请帮助Java 结束do-while循环,java,do-while,Java,Do While,好的,我有一个do-while循环,应该在使用点击'q'时结束,但是它给了我错误消息,请帮助 package Assignments; import java.util.*; public class assignment3 { public static void main(String[] args) { //Scanner Scanner stdIn = new Scanner(System.in); //Variables
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: " + 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) == '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");
}
}
包分配;
导入java.util.*;
公开课作业3{
公共静态void main(字符串[]args){
//扫描器
扫描仪标准输入=新扫描仪(System.in);
//变数
final double METERS_TO_CM=100;//将米转换为厘米的常数
最终双BSA_常数=3600;//BSA除以的常数
双倍体重指数;//体重指数
双倍重量;//重量单位为千克
双倍高度;//以米为单位的高度
字符串分类;//将用户分类为BMI类别
双bsa;//体表面积
System.out.print(“欢迎使用BMI和BSA计算器开始以千克为单位输入体重”);
重量=stdIn.nextDouble();
系统输出打印(“以米为单位输入高度:”;
高度=stdIn.nextDouble();
体重指数=体重/(身高*身高);//计算体重指数
bsa=Math.sqrt((高度*米到厘米)*重量)/bsa_常数);//计算bsa
如果(体重指数<18.5)
{
分类=“体重不足”;
}
否则,如果(体重指数<25)
{
分类=“正常”;
}
否则,如果(体重指数<30)
{
分类=“超重”;
}
其他的
{
分类=“肥胖”;
}
System.out.println(“选择下面的选项以设置高度和重量”);
System.out.println(“您的分类为:“+分类”);
系统输出打印项次(“(H)八:“+高度+米”);
系统输出打印项次(“(W)八:“+重量+”千克”);
System.out.printf(“BMI:%.1f\n”,BMI);
System.out.printf(“BSA:%.2f\n”,BSA);
系统输出打印项次(“(Q)uit”);
字符串响应=stdIn.next();
做{
如果(响应字符(0)='w')
{
System.out.println(“输入新重量:”);
重量=stdIn.nextDouble();
System.out.println(“选择下面的选项以设置高度和重量”);
System.out.println(“您的分类为:“+分类”);
系统输出打印项次(“(H)八:“+高度+米”);
系统输出打印项次(“(W)八:“+重量+”千克”);
System.out.printf(“BMI:%.1f\n”,BMI);
System.out.printf(“BSA:%.2f\n”,BSA);
系统输出打印项次(“(Q)uit”);
体重指数=体重/(身高*身高);
bsa=数学sqrt((高度*米到厘米)*重量)/bsa_常数);
response=stdIn.next();
}
else if(response.charAt(0)='h')
{
System.out.println(“输入新高度:”);
高度=stdIn.nextDouble();
System.out.println(“选择下面的选项以设置高度和重量”);
System.out.println(“您的分类为:“+分类”);
系统输出打印项次(“(H)八:“+高度+米”);
系统输出打印项次(“(W)八:“+重量+”千克”);
System.out.printf(“BMI:%.1f\n”,BMI);
System.out.printf(“BSA:%.2f\n”,BSA);
系统输出打印项次(“(Q)uit”);
体重指数=体重/(身高*身高);
bsa=数学sqrt((高度*米到厘米)*重量)/bsa_常数);
response=stdIn.next();
}
else if(response.charAt(0)!=“w”)
{
System.out.println(“这不是一个有效的选择,请重试”);
response=stdIn.next();
}
else if(response.charAt(0)!='h')
{
System.out.println(“该选项无效,请重试”);
response=stdIn.next();
}
else if(response.charAt(0)='q')
{
打破
}
}while(响应!=“q”);
}
}
试试看
else if(response.charAt(0).contains('q'))试试看
否则,如果(response.charAt(0).contains('q'))您必须具有以下内容:
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();
}
你必须有这样的东西
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();
}
问题在于这些线路:
else if (response.charAt(0)!= 'w')
// ...
else if (response.charAt(0)!= 'h')
当响应为“q”时,则满足每个测试。摆脱他们;他们什么都不做。相反,在最后一次有效的字符测试后加上一个普通的“else”,并打印“notavailable choice”提示符。问题在于这些行:
else if (response.charAt(0)!= 'w')
// ...
else if (response.charAt(0)!= 'h')
当响应为“q”时,则满足每个测试。摆脱他们;他们什么都不做。相反,在最后一次有效字符测试后加上一个简单的“else”,并打印“not a valid choice”(非有效选项)提示。
q
不等于w
或h
。所以,对于else if(response.charAt(0)!=“w')
来说,条件是真的,所以您不会将实际的else if(response.charAt(0)=“q')
条件中断
因此,以这种方式放置最后3个else if
-
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();
}
q
不等于w
或h
。所以,对于else if(response.charAt(0)!=“w')
来说,条件是真的,所以您不会将实际的else if(response.charAt(0)=“q')
条件中断
因此,以这种方式放置最后3个else if
-
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();
}
错误消息是什么?另外,您应该使用
!“q”.equals(response)
而不是==用于字符串比较没有错误,但当我输入q时,它只给出System.out.println(“这不是一个有效的选择,请重试”);不要打破循环,你应该考虑一些较小的方法,而不是把所有的逻辑放在一个大循环中——这将帮助你更仔细地推理