2017-05-21 15 views
0

私はクラスのプロジェクトに取り組んでいます。 C#で初めてのコーディングであり、問​​題が発生しました。私は、ロックペーパーはさみゲームのために数字(1,2、または3)をランダムに生成する必要がありますが、プログラムは乱数ではなく3を出力し続けます。ここに私のコードです。これがなぜ起こっているのかについての示唆はありますか?C#ランダムでランダムな結果が生成されない

あなたがループ内で乱数生成を配置する必要があり
using System; 
class elevator{ 
    public static void Main(string[] args){ 
     string response; 

     // Create an instance of the random class 
     Random random = new Random(); 

     // Return a random non negative interger. Max is set to 4 as it is exclusive and will set the true max to 3. 
     int compChoice = random.Next(1, 4); 


     Console.Write("Do you want to play Rock, Paper, Scissors? "); 
     response = Console.ReadLine(); 
     response = response.ToUpper(); 


     while(response == "YES"){ 

      // If statements displaying auto generated play for first player. 
      if(compChoice == 1){ 
       Console.WriteLine("First player <computer> Selection - Rock"); 
      } 
      if(compChoice == 2){ 
       Console.WriteLine("First player <computer> Selection - Paper"); 
      } 
      if(compChoice == 3){ 
       Console.WriteLine("First player <computer> Selection - Scissors"); 
      } 

      // Allow user to make selection 
      Console.Write("Second Player Selection - <Type 1,2, or 3. Rock = 1, Paper = 2, or Scissors = 3>: "); 
       int secondPlayer = Convert.ToInt32(Console.ReadLine()); 

      // Determine Winner 
      if (secondPlayer == 1 & compChoice == 1) { 
       Console.WriteLine("You both chose rock!"); 
      } 
      if (secondPlayer == 1 & compChoice == 2) { 
       Console.WriteLine("Player two wins! Paper covers rock."); 
      } 
      if (secondPlayer == 1 & compChoice == 3) { 
       Console.WriteLine("Player one wins! Rock smashes scissors."); 
      } 
      if (secondPlayer == 2 & compChoice == 1) { 
       Console.WriteLine("Player one wins! Paper covers rock."); 
      } 
      if (secondPlayer == 2 & compChoice == 2) { 
       Console.WriteLine("You both chose paper!"); 
      } 
      if (secondPlayer == 2 & compChoice == 3) { 
       Console.WriteLine("Player two wins! Scissors cut paper."); 
      }   
      if (secondPlayer == 3 & compChoice == 1) { 
       Console.WriteLine("Player two wins! Rock smashes scissors."); 
      } 
      if (secondPlayer == 3 & compChoice == 2) { 
       Console.WriteLine("Player one wins! Scissors cut paper."); 
      } 
      if (secondPlayer == 3 & compChoice == 3) { 
       Console.WriteLine("You both chose scissors!"); 
      } 

      // Ask user if they want to play another round 
      Console.Write("Do you want to play Rock, Paper, Scissors? "); 
      response = Console.ReadLine(); 
      // Convert response to all caps 
      response = response.ToUpper(); 

     } 
    Console.WriteLine("Thanks for playing!"); 
    } 
} 
+0

ループに番号生成を移動する必要があります。また、「ランダム」は仕様ごとにランダムではありません。それをお読みください。それを別々に初期化してみてください。あなたの場合、これは次のようになります: 'ランダムランダム=新しいランダム(DateTime.Now.Milliseconds);' –

+0

_Random()は仕様ごとにランダムではありません。それを読んでください._あなた自身に従うべき賢明な言葉。 – TaW

+0

'Random'は乱数ではなく擬似乱数を生成します。統計を使用して「乱数」を計算します。したがって、ランダムではありません。異なる数字で播種した場合、ランダムであるように見えます。とにかく、この質問とは無関係で、ちょっとしたメモです。この問題に関連する問題は、@Alexの答えに基づくコードの問題です。 –

答えて

5

while (response == "YES") { 
    int compChoice = random.Next(1, 4); 

そうでなければ、それは一度番号を生成し、その1つのすべての時間

参照random.Nextとしてかかります2番目のプレイヤーのConsole.ReadLine()で実行したのと同じように、「ランダム、次の乱数を取得できますか?」

+1

完璧!どうもありがとう。 – DMellon

+1

あなたは大歓迎です –

関連する問題