随机数猜测游戏答案0

时间:2016-10-12 03:42:19

标签: c# random

我正在创建一个随机数猜测游戏,用户可以设置他们想要猜测的最大数量。我已经找到了大部分代码,我似乎得到的问题是,在设置了最大数量并开始猜测答案总是为0之后。有关如何调整我的代码以便答案是一个数字的任何提示范围而不是0?

class Program
{

    public static int SelectedNumber = 0;
    public static Random ran = new Random();
    public static bool GameOver = false;
    public static int UserMaxValue = 0; 
    public static string decision;

    static void Main(string[] args)
    {

        int UserNumber;
        SelectedNumber = ran.Next(0, UserMaxValue);

        do
        {
            Console.WriteLine("What is the maximum number you want to guess from?");
            UserMaxValue = Convert.ToInt32(Console.ReadLine());

            do
            {
                Console.WriteLine("Select a number between 1 and {0}!", UserMaxValue);
                UserNumber = Convert.ToInt32(Console.ReadLine());
                GuessNumber(UserNumber);
            } while (GameOver == false);
        } while (GameOver == false);
    }

    public static void GuessNumber(int UserNumber)
    {

        if (UserNumber < SelectedNumber)
            Console.WriteLine("Your number is wrong, please try again!");
        else if (UserNumber > SelectedNumber)
            Console.WriteLine("Your Number is wrong, please try again!");
        //else if (UserNumber == 0)
        //    Console.WriteLine("Your Number is wrong, please try again!");
        else
        {
            Console.WriteLine("Yay! You got the right number. Do you want to play again? (y/n) ", decision);
            decision = Console.ReadLine();

            if (decision == "n")
                GameOver = true;
            else
                do
                {
                    Console.WriteLine("What is the maximum number you want to guess from?");
                    UserMaxValue = Convert.ToInt32(Console.ReadLine());

                    do
                    {
                        Console.WriteLine("Select a number between 1 and {0}!", UserMaxValue);
                        UserNumber = Convert.ToInt32(Console.ReadLine());
                        GuessNumber(UserNumber);
                    } while (GameOver == false);
                } while (GameOver == false);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

按顺序排列以下行(我省略其余部分):

public static int UserMaxValue = 0; 
// ...
SelectedNumber = ran.Next(0, UserMaxValue);
// ...
Console.WriteLine("What is the maximum number you want to guess from?");
        UserMaxValue = Convert.ToInt32(Console.ReadLine());

在正确设置SelectedNumber之前,您必须要求用户提供UserMaxValue。

答案 1 :(得分:1)

必须对您的代码进行一些更正。为更改添加了一些注释。 您最好在用户输入上添加错误处理。

更新:为无效输入添加了错误处理。

class Program
{
    public static int SelectedNumber = 0;
    public static Random ran = new Random();
    public static bool GameOver = false;
    public static int UserMaxValue = 0;        

    static void Main(string[] args)
    {
        int UserNumber = 0;
        bool playAgain = false;
        do
        {
            bool isUserMaxValueValid = false;
            do
            {
                Console.WriteLine("What is the maximum number you want to guess from?");
                isUserMaxValueValid = int.TryParse(Console.ReadLine(), out UserMaxValue);
            } while (!isUserMaxValueValid);

            SelectedNumber = ran.Next(1, UserMaxValue); // Re-assign SelectedNumber for every new max number input. Random number start changed to 1 to avoid 0 value for SelectedNumber

            do
            {
                bool isUserNumberValid = false;
                do
                {
                    Console.WriteLine("Select a number between 1 and {0}!", UserMaxValue);
                    isUserNumberValid = int.TryParse(Console.ReadLine(), out UserNumber);
                } while (!isUserNumberValid);

                playAgain = GuessNumber(UserNumber);
                // I changed GameOver to see if the guessing portion is finished or not
            } while (!GameOver); // You don't need to use GameOver == false
        } while (playAgain); // Check if user wants to play again or not
    }


    public static bool GuessNumber(int UserNumber)
    {
        if (UserNumber != SelectedNumber)
        {
            GameOver = false;
            Console.WriteLine("Your number is wrong, please try again!");
        }
        else
        {
            GameOver = true;
            bool isPlayAgainValid = false;
            Console.WriteLine("Yay! You got the right number. Do you want to play again? (y/n)");
            do
            {
                string decision = Console.ReadLine();

                if (decision.Equals("n", StringComparison.InvariantCultureIgnoreCase))
                {
                    return false;
                }
                else if (decision.Equals("y", StringComparison.InvariantCultureIgnoreCase))
                {
                    break;
                }

                if(!isPlayAgainValid)
                {
                    Console.WriteLine("Please enter y or n only.");
                }
            } while (!isPlayAgainValid);               


            // I removed redundant code
        }

        return true;
    }
}