-3

広範なコードに反対票を投じる前に、解決策を提示する必要があると感じています。

number以前に提示されたヘルプからプログラムのコードを変更しましたが、乱数が適切に比較されていないという問題にまだ陥っているようです( number「5」とユーザーの推測の例がありました)。は '5' ですが、「あなたはかなり離れています! もう一度やり直してください。」というコメントをまだ受け取っています。else if (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!");


                }


            }
        }

    }
4

2 に答える 2

2

OK、ここにはかなりの問題があると思います(トピックから外れているものもありますが、言及する価値は間違いありません)。

  1. while ループを使用して特定の入力をチェックすることはお勧めしません

    roundCount != 11例:使用する代わりにroundCount < 11

    そうすれば、永久にループに陥る可能性が低くなります

  2. ループの外側で Random を宣言する必要があります。そうしないと、同じ数値を取得するリスクがあります (「ランダムに」)。

  3. ユーザーが正しい数字を推測する機会がないように、推測するたびに数字を新しい数字にリセットします

以上のことを踏まえて、数値から離れた距離を見つけようとしている場合、Math.Abs​​ は正しかったと思います.2 未満は使用しませんが、答えから 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 に答える