Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 随机数猜游戏答案0_C#_Random - Fatal编程技术网

C# 随机数猜游戏答案0

C# 随机数猜游戏答案0,c#,random,C#,Random,我正在创建一个随机数字猜测游戏,用户在其中设置他们想猜测的最大数字。我已经找出了大部分代码,我似乎遇到的问题是,在设置了最大值并开始猜测答案后,答案总是0。关于如何调整我的代码,使答案是范围内的数字而不是0,有什么建议吗 class Program { public static int SelectedNumber = 0; public static Random ran = new Random(); public static bool GameOver = f

我正在创建一个随机数字猜测游戏,用户在其中设置他们想猜测的最大数字。我已经找出了大部分代码,我似乎遇到的问题是,在设置了最大值并开始猜测答案后,答案总是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);
            }
        }
    }
}
类程序
{
公共静态int SelectedNumber=0;
public static Random ran=new Random();
公共静态bool GameOver=false;
公共静态int UserMaxValue=0;
公共静态字符串决策;
静态void Main(字符串[]参数)
{
int用户号;
SelectedNumber=ran.Next(0,UserMaxValue);
做
{
WriteLine(“您想从中猜测的最大数字是多少?”);
UserMaxValue=Convert.ToInt32(Console.ReadLine());
做
{
WriteLine(“选择一个介于1和{0}之间的数字!”,UserMaxValue);
UserNumber=Convert.ToInt32(Console.ReadLine());
猜数(UserNumber);
}while(GameOver==false);
}while(GameOver==false);
}
公共静态无效猜测编号(int UserNumber)
{
if(UserNumber所选号码)
控制台。WriteLine(“您的号码错了,请再试一次!”);
//else if(UserNumber==0)
//控制台。WriteLine(“您的号码错了,请再试一次!”);
其他的
{
Console.WriteLine(“耶!你的号码对了。你想再玩一次吗?(y/n)”,决定);
decision=Console.ReadLine();
如果(决定=“n”)
GameOver=true;
其他的
做
{
WriteLine(“您想从中猜测的最大数字是多少?”);
UserMaxValue=Convert.ToInt32(Console.ReadLine());
做
{
WriteLine(“选择一个介于1和{0}之间的数字!”,UserMaxValue);
UserNumber=Convert.ToInt32(Console.ReadLine());
猜数(UserNumber);
}while(GameOver==false);
}while(GameOver==false);
}
}
}
}

您有以下几行,按此顺序排列(我省略了中间的其余部分):


在正确设置SelectedNumber之前,您必须向用户询问UserMaxValue。

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

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

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;
    }
}

此外,ran.Next只调用过一次,应该在每次用户要求再次播放时调用。如果最后显示相同的消息,则检查较大/较小的数字似乎是多余的(除非“数字”中的情况是故意的)。要么给出明确不同的消息以指导用户使用二进制搜索,要么加入他们!=条件此外,在ConsoleWriteLine中使用decision作为第二个参数没有任何作用,因为字符串没有复合格式。我更喜欢使用GuessNumber返回GameOver值,而在GameOver循环之外使用PlayOver decision。您还可以缩短
返回决策!=“n”
对于PlayAgainI,我认为我还需要在代码中的某个地方使用TryParse,以便在用户输入数字以外的内容时发出错误消息。我只是不知道应该在哪里以及如何写?@j_1311我更新了我的答案,并添加了一个错误处理示例。
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;
    }
}