Java 中的多else if 问题
关注:50 答案:6 mip版
解决时间 2021-01-17 01:14
提问者不在服务区的爱
2021-01-16 06:21
String tof1;
String tof2;
String tof3;
if (maximum >= 144);
{
tof1 = "true";
tof2 = "ture";
tof3 = "ture";
}
else if (maximum>=94&&maximum<144);
{tof1 = "false";
tof2 = "true";
tof3 = "true";
}
else if (maximum>=52&&maximum<94);
{
tof1 = "false";
tof2 = "false";
tof3 = "true";
}
else if (maximum<52)
{
tof1 = "false";
tof2 = "false";
tof3 = "false";
}
为什么 else if 中的else 有错误?
最佳答案
二级知识专家错过の花开
2021-01-16 07:36
if (maximum >= 144);
else if (maximum>=94&&maximum<144);
后面不要加分号
全部回答
1楼啾一口软脸
2021-01-16 10:43
lz每个
else if (maximum>=52&&maximum<94);
这样的后面都多了一个分号
分号表示结句,也就是说按照原本的程序 到分号这里就算完了,后面的{}中的内容与if无关了,所以后面就不应该出现else了
如果把分号都去掉,那么if后面{}里的才会被看成条件成立执行的语句,于是后面的else才会成立
lz有不明白的欢迎发消息
2楼你哪知我潦倒为你
2021-01-16 10:10
搞笑
3楼霸气ヽ爵爷
2021-01-16 09:55
public static void main(string[] args) { int x=5; if(x<3) { system.out.println("x<3"); } else if(x>=3 && x<8) { system.out.println("x>=3 && x<8"); } else { system.out.println("x>=3"); } int y; y=x>0?-x:x; system.out.println("y="+y); if(x==1) { if(y==1) { system.out.println("x==1,y!=1"); } else { system.out.println("x==1,y==1"); } } else { if(y==1) { system.out.println("x!=1,y==1"); } else { system.out.println("x!=1,y!=1"); } } } 这是改完的,我运行是没有问题了!~ 你程序大括号没对应上!~
4楼嘴硬欠吻
2021-01-16 08:43
换成
int maximum=0;
String tof1;
String tof2;
String tof3;
if (maximum >= 144) {
tof1 = "true";
tof2 = "ture";
tof3 = "ture";
} else if (maximum >= 94 && maximum < 144) {
tof1 = "false";
tof2 = "true";
tof3 = "true";
} else if (maximum >= 52 && maximum < 94) {
tof1 = "false";
tof2 = "false";
tof3 = "true";
} else if (maximum < 52) {
tof1 = "false";
tof2 = "false";
tof3 = "false";
}
错误原因:
if (maximum >= 144);
else if (maximum>=94&&maximum<144);
else if (maximum>=52&&maximum<94);
这三行后面的分号去掉。
5楼久溺深海心會寒
2021-01-16 08:12
";"表示语句结束,if语句后面只能跟一条语句,如果有";"就表示语句已经结了,再跟语句的话就已经不是在if里面的了
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
→点此我要举报以上信息!←
推荐资讯
大家都在看