我是 Java 的完全初学者,我一直在制作简单的测试程序来复习我读过的一些材料。以下代码块工作不正确。它应该接受来自用户的年、月和日期,然后创建一个用年、月和日期初始化的 GregorianCalendar 对象。但是,当我尝试返回 GregorianCalendar 变量的月份时,它总是返回我初始化月份变量的月份。我不确定为什么。
import java.util.*;
public class Prac {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Year: ");
int YEAR = input.nextInt();
System.out.print("Enter Month: ");
String MONTH_STRING = input.next();
System.out.print("Enter Date: ");
int DATE = input.nextInt();
int MONTH = 10;
String mon = MONTH_STRING.toLowerCase();
if (mon == "january") {
MONTH = 0;
} else if (mon == "february") {
MONTH = 1;
} else if (mon == "march") {
MONTH = 2;
} else if (mon == "april") {
MONTH =3;
} else if (mon == "may"){
MONTH =4;
} else if (mon == "june"){
MONTH =5;
} else if (mon == "july"){
MONTH =6;
} else if (mon == "august"){
MONTH=7;
} else if (mon == "september"){
MONTH=8;
} else if (mon == "october"){
MONTH=9;
} else if (mon == "november"){
MONTH=10;
} else if (mon == "december"){
MONTH =11;
}
GregorianCalendar entDate = new GregorianCalendar(YEAR,MONTH,DATE);
System.out.println(entDate.get(Calendar.MONTH));
}
}
另外,我知道我本可以使用 switch 块,但它以某种方式给了我奇怪的错误。