C# 猜游戏号码问题

C# 猜游戏号码问题,c#,C#,在我被广泛的代码否决之前,我觉得有必要提出一个解决方案 我已经根据前面提供的帮助对程序的代码进行了修改,但我似乎仍然陷入了这样一个问题:随机数数字或者没有被正确比较(我有过这样的例子,数字是'5',用户的猜测是'5',但我仍然得到了这样一条评论:“你离得太远了!再试一次。”意思是如果(userinputcalc>4 | userinputcalc

在我被广泛的代码否决之前,我觉得有必要提出一个解决方案

我已经根据前面提供的帮助对程序的代码进行了修改,但我似乎仍然陷入了这样一个问题:随机数
数字
或者没有被正确比较(我有过这样的例子,
数字
是'5',用户的猜测是'5',但我仍然得到了这样一条评论:“你离得太远了!再试一次。”意思是如果(userinputcalc>4 | userinputcalc<10).

因此,在这个阶段,问题似乎在于比较
number
userinput
,导致输出消息混乱

我可能在这里遗漏了一些明显的东西,尽管我确信这是在比较
number
userinput
的循环中,但我一直在看这段代码,却什么也没看到

我们将一如既往地感谢您的帮助

    public void GuessingGame()
        {
            string username; // Will be the user's chosen name for program interaction
            int guessesleft = 0;// Stands for the number of guesses left (out of 3)
            int spaceaway = 0; // Space from the guess and the random number, if not correct guess
            int roundcount = 1; //Started at 1 for the sake of the user interface - aesthetics
            int number = 0; // Current value of the random number
            int userinput = 0; //User input is the guess the user makes during the guessing game
            int userinputcalc = 0;// calculation of guess and random number, when added to spaceaway calculation
            int answersright = 0; // Number of times the user guessed the number correctly

            Random rndm = new Random(); // Initialises a new class of random, which'll be used to simulate the random number

            Console.WriteLine("Welcome to Guessing Game!");
            Console.WriteLine("");
            Console.WriteLine("Please press any button to continue.");
            Console.ReadLine();

            Console.WriteLine("What's your name?");
            username = (Console.ReadLine());
//If you're wondering at all, the "You must guess what it is inthree tries." is intentional, since it was showing double-spaced in the command prompt
            Console.WriteLine("Well, " + username + ", I am thinking of a number from 1 to 10. You must guess what it is inthree tries.");
            Console.WriteLine("");


            {
               do
                {
                    Console.WriteLine("Round" + roundcount); //Displays the which round (out of 10) to the user


                    guessesleft = 3; //The remaining guesses left for the user

                    do
                    {
                        number = rndm.Next(10) + 1; // int number is set to a random number between 1 and 10

                        Console.WriteLine("Please enter a guess:");
                        userinput = int.Parse(Console.ReadLine());
                        guessesleft = guessesleft - 1;


                        if (userinput == number)
                        {
                            //Below,  once you've guessed right, you will have this message displayed in the console
                            Console.WriteLine("You guessed " + number + " *RIGHT*!");
                            answersright = answersright + 1;
                            guessesleft = 0;// No point need to guess further on something you've guessed correctly - saves correct answer value exploit
                        }

                        else if (userinput < 1 || userinput > 10) // If user's guess is less than 1 or more than 10, then out of range. Counts as a guess.
                        {           
                            Console.WriteLine("You guessed " + userinput + "! and it was incorrect!");
                            Console.WriteLine("This is outside of the range of numbers between 1-10 ");

                        }


                        else if  (userinput != number) // while the user's guess does not equal the number
                        {
                            {
                                // userinputcalc = Math.Abs(number - userinput);  
                                //Left out as I was getting abnormal run-time outputs and the math showed up wrong.
                                //(Example: RND No. = 5 Userinput = 5 Output: "Incorrect" "Hot")

                                spaceaway = (number - userinput); // Works out how far from the random no. the user's guess is.
                                // If user guesses 6 and random no. is 5, answer will be -1 this makes the value +ve and allows output to be shown without error
                                if (spaceaway < 0)
                                {
                                    spaceaway = (spaceaway * -1);
                                    userinputcalc = spaceaway;
                                }

                                else if (spaceaway > 0)
                                {
                                    userinputcalc = spaceaway;
                                }

                            }

                            {
                                if (userinputcalc < 2)
                                {
                                    Console.WriteLine("You guessed " + userinput + "! and it was wrong!");
                                    Console.WriteLine("Hot");
                                }

                                else if
                                     (userinputcalc < 3)
                                {
                                    Console.WriteLine("You guessed " + userinput + "! and it was wrong!");
                                    Console.WriteLine("Warm");
                                }

                                else if
                                    (userinputcalc < 4)
                                {
                                    Console.WriteLine("You guessed " + userinput + "! and it was wrong!");
                                    Console.WriteLine("Cold");
                                }

                                else if (userinputcalc > 4 | userinputcalc < 10)
                                {
                                    Console.WriteLine("You guessed " + userinput + "! and it was wrong!");
                                    Console.WriteLine("You're quite far off! Try again.");
                                }
                            }
                        }

                    } while (guessesleft > 0);

                    Console.WriteLine("");
                    Console.WriteLine("The number was, "+number+"!");
                    Console.WriteLine("");

                    roundcount = roundcount + 1;

                } while (roundcount < 11);

                Console.WriteLine("Well, " + username + ". " +  "You guessed correctly, " + answersright + " times!");


                }


            }
        }

    }
public void猜测游戏()
{
字符串username;//将是用户为程序交互选择的名称
int guessleft=0;//表示剩余的猜测数(共3次)
int spaceaway=0;//与猜测和随机数之间的空格,如果不是正确的猜测
int roundcount=1;//为了用户界面的美观,从1开始
int number=0;//随机数的当前值
int userinput=0;//用户输入是用户在猜谜游戏中做出的猜测
int userinputcalc=0;//当添加到spaceaway计算时,计算猜测数和随机数
int answersright=0;//用户正确猜测数字的次数
Random rndm=new Random();//初始化一个新的Random类,该类将用于模拟随机数
控制台.WriteLine(“欢迎来到猜谜游戏!”);
控制台。写线(“”);
控制台。WriteLine(“请按任意按钮继续”);
Console.ReadLine();
控制台。WriteLine(“你叫什么名字?”);
用户名=(Console.ReadLine());
//如果您有任何疑问,那么“您必须在三次尝试中猜出它是什么”是故意的,因为它在命令提示中显示双倍行距
WriteLine(“嗯,“+username+”,我想的是一个从1到10的数字。你必须在三次尝试中猜出它是什么。”);
控制台。写线(“”);
{
做
{
Console.WriteLine(“Round”+roundcount);//向用户显示哪一轮(共10轮)
GuessLeft=3;//留给用户的剩余猜测
做
{
number=rndm.Next(10)+1;//int number设置为1到10之间的随机数
Console.WriteLine(“请输入猜测:”);
userinput=int.Parse(Console.ReadLine());
猜左=猜左-1;
if(userinput==number)
{
//下面,一旦您猜对了,您将在控制台中显示此消息
WriteLine(“你猜到了“+number+”*RIGHT*!”;
回答权=回答权+1;
GuessLeft=0;//无需进一步猜测正确猜测的内容-保存正确答案值
}
else if(userinput<1 | | userinput>10)//如果用户的猜测小于1或大于10,则超出范围。计为猜测。
{           
WriteLine(“你猜到了“+userinput+”!这是不正确的!”);
Console.WriteLine(“这超出了1-10之间的数字范围”);
}
else if(userinput!=number)//而用户的猜测不等于数字
{
{
//userinputcalc=Math.Abs(number-userinput);
//因为我得到了异常的运行时输出,而数学结果显示错误,所以被忽略了。
//(示例:RND编号=5用户输入=5输出:“不正确”“热”)
spaceaway=(number-userinput);//计算出与随机编号的距离。用户的猜测是。
//如果用户猜6,随机数是5,答案将是-1,这使得值为+ve,并允许输出显示无误
if(spaceaway<0)
{
spaceaway=(spaceaway*-1);
userinputcalc=spaceaway;
}
否则如果(空格>0)
{
userinputcalc=spaceaway;
}
}
{
if(userinputcalc<2)
{
WriteLine(“你猜到了“+userinput+”!它错了!”);
控制台。写入线(“热”);
}
否则如果
(userinputcalc<3)
{
WriteLine(“你猜到了“+userinput+”!它错了!”);
控制台。WriteLine(“温暖”);
}
否则如果
 number = rndm.Next(10) + 1;  //Insert here
 do
 {
      //Displays the which round (out of 10) to the user 
      Console.WriteLine("Round" + roundcount); 
      guessesleft = 3; //The remaining guesses left for the user
      do
      {
          // Remove this -- number = rndm.Next(10) + 1; 
        static int guessesleft;
        static Random ran = new Random();
        static int MagicNumber;
        static string UserInput;

        static void Main(string[] args)
        {
            ConsoleKeyInfo ci = new ConsoleKeyInfo();
            do
            {
                guessesleft = 3;
                UserInput = "";
                Console.Clear();
                string username;
                int guesscount = 1;
                Console.WriteLine("Welcome to Jackie's Guessing Game!");
                Console.WriteLine("");
                Console.WriteLine("Please press any button to continue.");
                Console.ReadLine();

                Console.WriteLine("What's your name?");
                username = (Console.ReadLine());
                //If you're wondering at all, the "You must guess what it is inthree tries." is intentional, since it was showing double-spaced in the command prompt
                Console.WriteLine("Well, " + username + ", I am thinking of a number from 1 to 10. You must guess what it is inthree tries.");
                Console.WriteLine("");
                MagicNumber = ran.Next(1, 11);

                do
                {

                    Console.WriteLine("Please insert your " + guesscount++ + "º guess!");
                    UserInput = Console.ReadLine().Trim();
                    if (UserInput == MagicNumber.ToString())
                        break;

                    if (Math.Abs(Convert.ToInt32(UserInput) - MagicNumber) < 2)
                        Console.WriteLine("Hot!!");
                    else if(Math.Abs(Convert.ToInt32(UserInput) - MagicNumber) < 3)
                        Console.WriteLine("Warm!!");

                    Console.WriteLine("No luck , you have " + --guessesleft + "º guesses left!");
                    Console.WriteLine();

                } while (guesscount < 4);

                if (guesscount == 4)
                    Console.WriteLine("Sorry " + username + " no more tries,you lost!");
                else
                    Console.WriteLine("Congratulations your guess with number " + UserInput + " is correct,the number was " + MagicNumber);

                Console.WriteLine("Press Q to quit or Enter to Play again!");
                ci = Console.ReadKey();
            } while (ci.Key != ConsoleKey.Q);  

        }