我已經制定了一個計劃,以告知哪個季節是基於當月。然而,無論我輸入什麼,它都說它是墮落的。下面是代碼:Java問題:如果/其他
import java.util.Scanner;
public class SeasonChecker {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
System.out.println("What month is it??");
String month = input.nextLine();
System.out.println(month);
if (month == "december"||month == "January"||month=="February"){
System.out.println("Then it is Winter?");
}
else if (month=="March"||month=="May"||month=="April"){
System.out.println("Then it is Spring!!!");
}
else if (month=="June"||month=="July"||month=="August"){
System.out.println("Then it is Summer!");
}
else {
System.out.println("Then it is Autumn!");
}
input.close();
}
}
不要將字符串值與'=='進行比較;比較對象引用以確定它們是否是同一個對象。使用'equals'來比較字符串值。 – rgettman