2014-10-27 79 views
1

我正在爲一個類作業製作一個搖滾紙剪刀遊戲,並且我正在嘗試編寫一個方法,根據用戶是贏,計算機獲勝還是配合,返回整數1,2或3。我們不允許簡單地回覆「你贏了!」的消息。或「你輸了!」或「Tie!」在方法中,我們必須返回整數。但是,當我按照這種方式鍵入代碼時,在Eclipse中出現編譯器錯誤警告,告訴我「此方法必須返回int類型的結果。」不是我在這裏做什麼?幫幫我! 謝謝!Java - 爲什麼Eclipse告訴我我的方法必須返回int類型?

public static int playGame(String userChoose) { 
    System.out.println("Enter your choice."); 
    userChoose=kbd.nextLine().toLowerCase(); 
    String computerChoose=computerChoose(); 
    if (userChoose.equals(computerChoose)) { 
     return 3; 
    } 
    else if (userChoose.equals("rock")) { 
     if (computerChoose.equals("scissors")) 
      return 1; 
     else if (computerChoose.equals("paper")) 
      return 2; 
    } 
    else if (userChoose.equals("paper")) { 
     if (computerChoose.equals("scissors")) 
      return 2; 
     else if (computerChoose.equals("rock")) 
      return 1; 
    } 
    else if (userChoose.equals("scissors")) { 
     if (computerChoose.equals("paper")) 
      return 1; 
     else if (computerChoose.equals("rock")) 
      return 2; 
    } 
} 
+0

您的if-else案例不涵蓋所有案例。爲其餘結果添加一個'else'的情況並返回一個int。 – Raptor 2014-10-27 03:13:01

回答

1

由於您return語句都是你裏面的if-else子句之一,
你需要的時候沒有你的if-else條件滿足時給予默認返回值的情況。
喜歡的東西:

public static int playGame(String userChoose) { 
    System.out.println("Enter your choice."); 
    userChoose=kbd.nextLine().toLowerCase(); 
    String computerChoose=computerChoose(); 
    if (userChoose.equals(computerChoose)) { 
     return 3; 
    } 
    else if (userChoose.equals("rock")) { 
     if (computerChoose.equals("scissors")) 
      return 1; 
     else if (computerChoose.equals("paper")) 
      return 2; 
    } 
    else if (userChoose.equals("paper")) { 
     if (computerChoose.equals("scissors")) 
      return 2; 
     else if (computerChoose.equals("rock")) 
      return 1; 
    } 
    else if (userChoose.equals("scissors")) { 
     if (computerChoose.equals("paper")) 
      return 1; 
     else if (computerChoose.equals("rock")) 
      return 2; 
    } 
    return -1; //--> the default return value 
} 
+1

謝謝!這就是訣竅! – Squidicide 2014-10-27 03:01:52

+0

很棒@Squidicide,快樂編碼! – 2014-10-27 03:04:07

0

編譯器不知道輸入必須是岩石,紙張或剪刀。如果你守護的輸入,則

else if (userChoose.equals("scissors")) { 

應該只是

else { 

或者,在方法的末尾添加默認錯誤回報像

return -1; 
3

因爲你答應你會!

public static int playGame(String userChoose)

在那裏int說:「我敢發誓,我發誓我會返回一個int」

如果不兌現這一承諾編譯器變得脾氣暴躁。

這意味着無論您通過代碼取什麼分支,您都必須返回int。如果

0

這些都不如果被驗證

你必須提供一個返回值來解決這個問題

public static int playGame(String userChoose) { 
System.out.println("Enter your choice."); 
userChoose=kbd.nextLine().toLowerCase(); 
String computerChoose=computerChoose(); 
if (userChoose.equals(computerChoose)) { 
    return 3; 
} 
else if (userChoose.equals("rock")) { 
    if (computerChoose.equals("scissors")) 
     return 1; 
    else if (computerChoose.equals("paper")) 
     return 2; 
} 
else if (userChoose.equals("paper")) { 
    if (computerChoose.equals("scissors")) 
     return 2; 
    else if (computerChoose.equals("rock")) 
     return 1; 
} 
else if (userChoose.equals("scissors")) { 
    if (computerChoose.equals("paper")) 
     return 1; 
    else if (computerChoose.equals("rock")) 
     return 2; 
} 
else 
return -1; 

}

0

它總是最好使用像一個返回值的變量,然後使用條件設置它,然後在代碼結束時返回它。例如:

int retValue = 0; 
... 
else if (userChoose.equals("scissors")) { 
    if (computerChoose.equals("paper")) 
     retValue = 1; 
    else if (computerChoose.equals("rock")) 
     retValue = 2; 
    } 
} 
return retValue; 
0

簡短的回答已經被其他人給出了:你的有條件的條款並沒有涵蓋所有可能的狀態。

稍長的答案:任何時候你的代碼處理外部輸入,你必須考慮輸入是錯誤的可能性。這是編程的核心原則之一。

相關問題