else if(18<age<40) 為什么是錯誤表達(dá)?
public static void main(String[] args) { int age=25; if (age>60) { ? ?System.out.println("老年"); }else if (40<age<60) { ? ?System.out.println("中年"); ? ? }else if(18<age<40) { ? ?System.out.println("少年"); }else { ? ?System.out.println("童年"); } ? ?} } 為什么這是錯誤表達(dá)
? public static void main(String[] args) {
int age=25;
if (age>60)
{
? ?System.out.println("老年");
}else if (40<age<60)
{
? ?System.out.println("中年");
? ?
}else if(18<age<40)
{
? ?System.out.println("少年");
}else
{
? ?System.out.println("童年");
}
? ? }
}
為什么是錯誤表達(dá)
2018-03-11
else if ((age>18)&&(age<40))? 應(yīng)該這樣寫
2018-03-11
java不能這么寫,只能寫成else if(age>18&&age<40),而且你這個判斷不需要兩邊都寫,第一次判斷過不大于60歲之后就只能小于等于60歲了,也就是不需要再寫上界了。