最后一個條件,使用else還是else if 求解!看前面講解是else 但,用else顯示錯誤。下面加了if,顯示成功
public class HelloWorld {
? ? public static void main(String[] args) {
int age=25;
if (age>60){
? ? System.out.println("老年");
} else if (age<=60&&age>40){
? ? System.out.println("中年");
} else if (age<=40&&age>18){
? ? System.out.println("少年");
} else if (age<=18){
? ? System.out.println("童年");
}? ??
}
}
2019-10-28
因為用else的話,后面直接是代碼塊,但是你寫了(age<=18)這個條件,就得用else if。
else{代碼塊}或者else if(條件){代碼塊};而且你前幾個else if可以不用寫<=的內容,因為else if本身的意思就是不滿足上一個條件