2016-12-20 16 views
-3

私はこのコードをコンパイルしようとしていますが、judgeRockPaperScissors()に「すべてのコードパスが値を返すわけではありません」というメッセージが表示されます。私は、メインメソッドで動作する3つのメソッドが必要です。何が間違っているのか分かりません。 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チェックを追加して、nullを返さないようにすることもできます。

+0

https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-明示的に例外をスローして、プログラムの何が間違っているかを知ることをお勧めします。 – silentsod

+0

私は同意する、BradleyDotNETの答えははるかに良いです。 @silentsodのリンクと情報ありがとうございました –

4

intの範囲が0-2をはるかに超えているため、コンパイラはif/else ifブロックのすべてのケースを処理したことをコンパイラには知らせていません。入力が無効なスローは例外ですので

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

:あなたのコードのコンパイルを取得する最も簡単な方法は、一般的なelseブロックを追加することです。 サイドノートでは、文字列を使用することは間違いなく適切なアプローチではありません。

+0

ありがとうございました。プログラムは今コンパイルされますが、プログラムを実行すると "Unhandled Exception:System.NotImplementedException:メソッドまたは操作が実装されていません"というエラーが表示されます。 judgeRockPaperScissors()から。 BradleyDotNET、私は文字列の私の使用に関するあなたのコメントは私の問題の一部であると仮定します。うまくいけば私はそれを把握することができます。 –

+0

@IsaacElderそれはあなたの 'private static int judgeRockPaperScissors()'バージョンから来ています。文字列はスタイル/練習が悪いが、その例外とは無関係です。 – BradleyDotNET

関連する問題