JAVA if语句工作和不工作';我不能同时工作
问题是我的if语句。第二个if按设计工作,它打印出我要求的内容,但第一个if不打印,即使它们的格式相同,并且默认为else语句。我只是想知道我做错了什么 第一个如果JAVA if语句工作和不工作';我不能同时工作,java,if-statement,Java,If Statement,问题是我的if语句。第二个if按设计工作,它打印出我要求的内容,但第一个if不打印,即使它们的格式相同,并且默认为else语句。我只是想知道我做错了什么 第一个如果 if(systemChoice.equals("M")){ weight = height * height * 25; outputLabel.setText(name + ", your ideal weight is " + weight + " kgs."); 第二个如果
if(systemChoice.equals("M")){
weight = height * height * 25;
outputLabel.setText(name + ", your ideal weight is " + weight + " kgs.");
第二个如果
if(systemChoice.equals("I")){
weight = height * height * 25 / 703;
outputLabel.setText(name + " , your ideal weight is " + weight + " lbs");
}
如果有必要,这里是整个程序的上下文
String name, systemChoice;
double height, weight;
name = nameInput.getText();
systemChoice = systemInput.getText();
height = Double.parseDouble(heightInput.getText());
/*Calculates your ideal weight
*based on a bmi formula in
*your preferred system of measurement
*Imperial or Metric
*/
if(systemChoice.equals("M")){
weight = height * height * 25;
outputLabel.setText(name + ", your ideal weight is " + weight + " kgs.");
}
if(systemChoice.equals("I")){
weight = height * height * 25 / 703;
outputLabel.setText(name + " , your ideal weight is " + weight + " lbs");
}
else{
outputLabel.setText("Error. Check your System input and try again.");
}
}
您的第二个if(连同它的else分支)总是执行,因为您的第一个if没有else分支。若要修复此问题,请在以下情况下将else分支添加到第一个分支:
if (...) {
...
} else if (...) {
...
} else {
...
}
Offel先生你应该在第二个if中使用else if
每次输入M时,第一个if实际起作用,并根据需要更改文本。但是第二个if将始终失败并打印else块。输入是什么?输出是什么?运行时错误还是编译时错误
systemChoice
不能同时是“M”和“I”…您能提供更多信息吗?问题已经清楚地解释了:第二个如果按设计工作,它会打印出我要求的内容,但第一个不会,即使它们的格式相同,并且默认为else语句。Man。现在我觉得自己像个白痴。谢谢你的回答。