Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

Guessing game player 2 loop code problem C#

My code is not recognizing int.tryparse within the player 2 loop so the player is only able to enter numbers between 0 and 100, when i enter any input it prints player1s input and says i win, im not understanding what is going wrong or what is not working in the code, Please Help!!!

static void Main()
{
    //Rules of the Guessing Game
    int MinValue = 0;
    int MaxValue = 100;
    Console.WriteLine("Welcome to the number gussing game!");
    Console.WriteLine("Player 1 will enter a number between 0 and 100(whole numbers only please)");
    Console.WriteLine("Player 2 will have 5 guesses to figure out which number palyer 1 picked");
    Console.WriteLine("After each of player 2's guesses you'll be told higher if your guess was to low and lower if your guess was to high.");
    Console.WriteLine("Player 2 wins if the number is guessed before they run out of guesses");
    Console.WriteLine("Either player can restart the game by typing "retry" at any time or quit the game by typing "quit" or "exit"");
    Console.WriteLine($"
Press any "key" to continue...");
    Console.ReadKey();
    //Player 1 Loop
    bool StillRunning = true;
    do
    {
        Console.Clear();
        Console.WriteLine($"Player 1 Please enter a number between {MinValue} and {MaxValue}");
        string Input = Console.ReadLine();
        string CheckForKeyWords = Input.ToLower();
        //If the user wants to quit the console application do this
        if (CheckForKeyWords == "quit")
        {
            break;
        }
        //If the user want to resart the application do this
        else if (CheckForKeyWords == "retry")
        {
        }

        int Number = MinValue - MaxValue;
        //if they type a number between 0 and 100 that is an integer do this
        if (int.TryParse(Input, out Number) == true)
        {
            Console.WriteLine($"You Chose {Input}");
        }

        //If they type a word instead of a number between 0 and 100 do this
        while (int.TryParse(Input, out Number) == false)
        {
            Console.WriteLine("Please Choose a number between 0 and 100(Intergers only please!)");
            Input = Console.ReadLine();
        }

        Console.WriteLine("
Press any "key" to continue...");
        Console.ReadKey();
        Console.Clear();
        string guess = "";
        int guessCount = 0;
        int guessLimit = 5;
        bool outOfGuesses = false;
        while (guess != Input && !outOfGuesses)
        {
            if (guessCount < guessLimit)
            {
                Console.WriteLine("Player 2 Please enter a guess.");
                guess = Console.ReadLine();
                guessCount++;
            }

            if (guessCount == guessLimit)
            {
                outOfGuesses = true;
            }
            else if (CheckForKeyWords == "retry")
            {
            }

            int Num = MinValue - MaxValue;
            if (int.TryParse(Input, out Num) == true)
            {
                Console.WriteLine($"You Chose {Input}");
            }

            while (int.TryParse(Input, out Num) == false)
            {
                Console.WriteLine("Please Choose a number between 0 and 100(Intergers only please!)");
                Input = Console.ReadLine();
            }
        }
    }
    while (StillRunning);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...