如果 else
紧跟在 if
后面,为什么这段代码可以工作:
if (GPA > 3.8);
else
System.out.println("words");
但是,如果我在“if”和“else”之间添加一条语句,例如:
if (GPA > 3.8);
System.out.println("words");
else
System.out.println("words");
我收到一条错误消息,指出 else
需要 if
?
最佳答案
分号终止 if
block ,将其删除
if (GPA > 3.8); // <-- here
应该是
if (GPA > 3.8)
关于java - if/else 不起作用并表示 else 需要 if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26286067/