猜猜游戏编号问题

时间:2013-08-04 21:53:31

标签: c#

在我为广泛的代码投票之前,我认为有必要找到解决方案。

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

所以,在这个阶段,问题似乎在于numberuserinput的比较,导致输出消息混乱。

我可能在这里遗漏了一些明显的东西,尽管我确定这是围绕着比较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!");


                }


            }
        }

    }

2 个答案:

答案 0 :(得分:2)

好的我觉得这里有很多问题(有些是偏离主题的,但肯定值得一提)..

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

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

    然后永远陷入循环的可能性更小

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

  3. 您在每次猜测后将号码重置为新号码,以便用户无法猜出正确的号码

  4. 说到这一切,我认为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; 
    

答案 1 :(得分: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);  

        }