我正在爲一個類作業製作一個搖滾紙剪刀遊戲,並且我正在嘗試編寫一個方法,根據用戶是贏,計算機獲勝還是配合,返回整數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;
}
}
您的if-else案例不涵蓋所有案例。爲其餘結果添加一個'else'的情況並返回一個int。 – Raptor 2014-10-27 03:13:01