-3
我已經編寫了一個靜態方法,該方法接受一個單一的int參數,該參數是數字月份並返回給定月份中的天數。因此,1的參數將返回31,因爲1月有31天,而參數2會返回28,因爲2月有28天,等等。無法在主要方法中調用靜態方法
但是,當我嘗試在我的主要方法中調用靜態方法時,出現錯誤消息void type not allowed here.
有人可以幫我弄清楚我做錯了什麼嗎?這是我到目前爲止:
public class Days {
public static void main(String[] args) {
System.out.println(daysInMonth(1));
}
public static void daysInMonth(int month) {
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
System.out.println("31");
else if (month==4||month==6||month==9||month==11)
System.out.println("30");
else if (month==2)
System.out.println("28");
}
}
你知道'return'值和方法調用之間的差異
int
值打印標準輸出的東西? –你有兩個選擇。或者只是調用'daysInMonth(1)'而不用周圍的'println'調用或者'daysInMonth(...)'返回'String' –
正如@SotiriosDelimanolis指出的,在'main'方法中,你調用'println' 'daysInMonth'方法的返回值,但該方法不返回值。該方法可以完成所有的打印,所以你的'main'方法不需要打印。或者'daysInMonth'方法應該返回一個具有天數的'int',並將'println'保存在'main'方法中。 – Colselaw