C#Random不会产生随机结果

时间:2017-05-21 22:20:10

标签: c# class random

我正在上课的项目。这是我第一次使用C#进行编码并遇到了问题。我需要为一个石头剪刀游戏随机生成一个数字(1,2或3),但程序保持输出3而不是随机数。这是我的代码。有关为何发生这种情况的任何建议?

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!");
    }
}

1 个答案:

答案 0 :(得分:5)

您需要将随机数生成放在循环中:

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

否则,它将生成一次该数字并始终使用该数字

请将random.Next视为&#34;随机,我可以获得下一个随机数&#34;,就像你为第二个玩家使用Console.ReadLine()所做的那样