2016-12-20 48 views
-3

我試圖編譯此代碼,但在judgeRockPaperScissors()中得到了「並非所有代碼路徑都返回了值」。我需要三種方法在主要方法中工作。我不知道什麼是錯的。我在將int轉換爲字符串時遇到了問題。任何幫助將是偉大的!謝謝!使用紙岩石剪刀編譯時出現接收錯誤

using System; 
using System.Windows.Forms; 

class RockPaperScissors 
{ 
static string response; 
static string respond; 
static string player1Sel; 
static int player2Sel; 
static int result; 
static Random numberGenerator = new Random(); //Generates a random number. 

public static void Main(string[] args) 
{ 
    Console.Write("Do you want to play Rock, Paper, Scissors?");// User writes yes or anything else. 
    respond = Console.ReadLine(); 
    respond = respond.ToUpper(); //Makes the responce uppercase. 
    while (respond == "YES") 
    { //Beginning of "while loop". 
     player1Sel = promptForInput(); 
     player2Sel = generateAutoSelect(); 
     result = judgeRockPaperScissors(); 
     switch (result) 
     { 
      case 00: 
       Console.WriteLine("Draw!"); 
       break; 
      case 12: 
       Console.WriteLine("Paper covers rock. Player 2 Wins!"); 
       break; 
      case 23: 
       Console.WriteLine("Scissors cut paper. Player 2 Wins!"); 
       break; 
      case 31: 
       Console.WriteLine("Rock smashes scissors. Player 2 Wins!"); 
       break; 
      case 13: 
       Console.WriteLine("Rock smashes scissors. Player 1 Wins!"); 
       break; 
      case 21: 
       Console.WriteLine("Paper covers rock. Player 1 Wins!"); 
       break; 
      case 32: 
       Console.WriteLine("Scissors cut paper. Player 1 Wins!"); 
       break; 
     }// End of switch. 

     Console.Write("Do you want to play again?");// Where the player decides to play again. 
     respond = Console.ReadLine(); 
     respond = respond.ToUpper(); 

    } //End of "while loop". 

} //End of Main. 

private static int judgeRockPaperScissors() 
{ 
    throw new NotImplementedException(); 
} 

public static string promptForInput() 
{ 
    Console.Write("Player one, make a selection. Type 1=rock, 2=paper, or 3=scissors and press enter: "); 
    player1Sel = Console.ReadLine(); 
    if (player1Sel == "") 
    { 
     MessageBox.Show("You must select a valid choice."); 
     Environment.Exit(0); 
    } 
    else 
    if (int.Parse(player1Sel) < 1 | int.Parse(response) > 3) 
    { 
     MessageBox.Show(response + " - is not a valid choice."); 
     Environment.Exit(0); 
    } 
    return player1Sel; 
}// End of promptForInput. 

public static int generateAutoSelect() 
{ 
    int player2Sel = numberGenerator.Next(1, 4);//Generates random number between 1 and 3. 

    if (player2Sel == 1) 
    { 
     MessageBox.Show("Player2 chose rock."); 
    } 
    else 
     if (player2Sel == 2) 
    { 
     MessageBox.Show("Player2 chose paper."); 
    } 
    else 
      if (player2Sel == 3) 
    { 
     MessageBox.Show("Player2 chose scissors."); 
    } 
    return player2Sel; 
} // End of generateAutoSelect. 

public static string judgeRockPaperScissors(int player1Sel, int player2Sel) 
{ 
    if (player1Sel == player2Sel) 
    { return "00"; } 
    else if (player1Sel == 1 && player2Sel == 2) 
    { return "12"; } 
    else if (player1Sel == 2 && player2Sel == 3) 
    { return "23"; } 
    else if (player1Sel == 3 && player2Sel == 1) 
    { return "31"; } 
    else if (player1Sel == 1 && player2Sel == 3) 
    { return "13"; } 
    else if (player1Sel == 2 && player2Sel == 1) 
    { return "21"; } 
    else if (player1Sel == 3 && player2Sel == 2) 
    { return "32"; } 
}// End of judgeRockPaperScissors. 

} //課程結束。

回答

0

更新的功能,如果沒有條件滿足時返回null:

public static string judgeRockPaperScissors(int player1Sel, int player2Sel) 
{ 
    if (player1Sel == player2Sel) 
    { return "00"; } 
    else if (player1Sel == 1 && player2Sel == 2) 
    { return "12"; } 
    else if (player1Sel == 2 && player2Sel == 3) 
    { return "23"; } 
    else if (player1Sel == 3 && player2Sel == 1) 
    { return "31"; } 
    else if (player1Sel == 1 && player2Sel == 3) 
    { return "13"; } 
    else if (player1Sel == 2 && player2Sel == 1) 
    { return "21"; } 
    else if (player1Sel == 3 && player2Sel == 2) 
    { return "32"; } 
    return null; 
} 

這樣,它會一直的東西回來。您還需要在主函數中爲結果變量添加一個空檢查,以確保它不返回null。

+0

https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare更好地拋出一個異常明確,讓你知道什麼出了問題的程序。 – silentsod

+0

我同意,BradleyDotNET的答案好多了。謝謝你的鏈接和信息雖然@silentsod –

4

編譯器不知道您已經處理了if/else if塊中的所有可能情況,因爲int的範圍遠遠大於0-2。讓你的代碼編譯最簡單的方法是添加一個通用的其他塊:

... 
else 
{ 
    throw new ArgumentException("Player selections out of range"); 
} 

由於輸入無效拋出一個異常。 在附註中,使用字符串的方式絕對不是正確的方法。

+0

謝謝你的幫助。該程序現在編譯,但是當運行該程序時,我仍然收到錯誤「未處理的異常:System.NotImplementedException:該方法或操作未實現。」來自judgeRockPaperScissors()。 BradleyDotNET,我認爲你對我使用字符串的評論是我的問題的一部分。希望我能弄明白。 –

+0

@IsaacElder這是來自你的'private static int judgeRockPaperScissors()'版本。字符串是不好的風格/練習,但與該例外無關。 – BradleyDotNET