2015-10-12 42 views
-1

所以我在編程類的介紹中被​​分配了一個問題。我做了一點研究,以完成這個問題,我能夠編譯沒有錯誤,但其中一個規定是我必須返回一個字符串。這是我把頭撞到一堵磚牆的地方。我嘗試了一些方法來解決這個問題,但我希望這裏有人能夠發現我一直在拉我的頭髮的問題。程序不會返回字符串

public class TellSeason { 

    public static void main(String[] args) { 
     season(5 , 12); 
    } 

    public static String season(int month , int day) { 

     if ((month == 9 && day >=16) 
      || month == 10 
      || month == 11 
      || (month == 12 && day <=15)) { 
      return ("Fall"); 
     } 
     else if ((month == 12 && day >=16) 
       || month == 1 
       || month == 2 
       ||(month == 3 && day <=15)) { 
      return ("Winter"); 
     } 
     else if ((month == 3 && day >=16) 
       || month == 4 
       || month == 5 
       ||(month == 6 && day <=15)) { 
      return ("Spring"); 
     } 
     else { 
      return("Summer"); 
     } 
    } 
} 
+0

的'是什麼|'的對?這不是爲我重申。 –

+0

程序的返回值總是int。你確定這個需求不僅僅是把一個字符串打印到標準輸出嗎? – Mureinik

+0

||是'或'操作數。但是我能夠完成這個問題 – UpTooLate

回答

1

如果你想從你的主返回任何東西,這是不可能的。 但是,如果你想顯示你的結果,System.out.println是你的方法

4

兩件事情,首先你是不是做你的方法的結果什麼。基本上,你只需在你的主要方法中調用季節,而對結果不做任何處理。其次,看看你的主要方法的方法簽名。它明確指出main具有返回類型void。這意味着,這種方法不能返回任何東西。您可以使用System.exit()提供退出代碼,但是,這僅限於整數返回代碼。

我強烈懷疑你真正所有的是將結果打印到控制檯的能力。也就是,

System.out.println(season(5,12)); 
+0

謝謝,我想我被這個問題的措詞誤導了。這絕對是一個DUH!時刻 – UpTooLate

+0

歡迎你,相信我,這恰好發生在我們所有人身上。如果我在編程時曾經有一英鎊的時間。我會相當有錢;-) – JustDanyul

5

是這樣的嗎?

public static void main(String[] args){ 
    System.out.println(season(5 , 12)); 
} 

還有一個提示 - 你可以比較的月和日一起

int idx = month * 100 + day; 
if (idx <= 315 || idx >= 1216) 
    return ("Winter"); 

if (idx >= 916) 
    return ("Fall"); 

if (idx >= 616) 
    return("Summer"); 

//if (idx >= 316) 
return ("Spring"); 
+0

完美工作,謝謝! – UpTooLate