我收到一个编译器错误,指出该变量nameOfMonth
可能尚未初始化。
我知道这可能是一个简单的解决方法,但我被卡住了,似乎无法在 Google 上找到答案。
public class Switch
{
public static void main ( String [] args )
{
int month;
String monthString, nameOfMonth;
month=ConsoleInput.readInt("Enter Month in the form, e.g- January = 1, Febuary = 2, etc");
monthString = nameOfMonth;
System.out.println(monthString);
}
protected static String nameOfMonth(int month)
{
String monthString;
switch(month)
{
case 1: monthString = "January";
break;
case 2: monthString = "Febuary";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid Month";
break;
}
return monthString;
}
}