-3

在我对大量代码投反对票之前,我觉得有必要提出一个解决方案。

我已经根据之前提供的帮助对程序的代码进行了修改,但我似乎仍然陷入了随机数number没有被正确比较的问题(我有例子,其中number'5' 和用户的猜测是'5',但我仍然收到一条评论说“你离得太远了!再试一次。”这意味着它正在落入else if (userinputcalc > 4 | userinputcalc < 10)...

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

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

一如既往地非常感谢任何帮助。

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


                }


            }
        }

    }
4

2 回答 2

2

好的,我认为这里有很多问题(有些是题外话,但绝对值得一提)..

  1. 我不建议使用 while 循环来检查特定的输入

    例如:而不是roundCount != 11使用roundCount < 11

    那么永远陷入循环的机会就更少了

  2. 您应该在循环之外声明 Random ,否则您将冒着获得相同数字的风险(“随机”)

  3. 每次猜测后将数字重置为新数字,这样用户就没有机会猜到正确的数字

话虽如此,我认为 Math.Abs​​ 是正确的,如果你试图找到与数字的距离。我不会使用少于两个的,因为这意味着只有距离答案 1 的数字是“热的”

注意:答案基于问题修订 #5


更新

看起来你离得不远,但你仍然在每个循环中重置数字

 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; 
于 2013-08-04T22:54:30.240 回答
0

我想这就是你想要的,请尝试一下:

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

        }
于 2013-08-04T22:57:39.830 回答