對於我的科學類我必須編寫一個程序來查找給定月份(1-12)中的天數,是否知道爲什麼我會收到錯誤「變量天可能沒有被初始化「當試圖從交換機返回int」天「?下面的代碼:因爲month
作爲參數傳遞給函數提供變量可能未被初始化
public static int getNumberofDays(int month,int year)
{
// Imports the required Scanner
Scanner kbd = new Scanner(System.in);
final String month;
final int days;
switch (month) {
case 1: days = 31;
break;
case 2: if ((year % 4 == 0) && year % 100 != 0)
{
days = 29;
}
else if ((year % 4 == 0) && (year % 100 == 0) && (year % 400 == 0))
{
days = 29;
}
else
{
days = 28;
}
break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 30;
break;
case 6: days = 31;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
default: month = "invalid";
break;
}
return days;
}
您發佈的代碼不準確。這引發了一個不同的編譯錯誤。 –
多麼棘手的問題!一種方法犯了很多錯誤,非常酷! – radoh