mercredi 7 octobre 2015

Getting a second input from same text box

I am in a C# class and our assignment is to create a random number generator and have the user guess the number. Once the user makes a guess the program tells the user if it is too high or low, then allows them to modify their guess. It also keeps track of the number of guesses a user has completed.

My issue is that after the first run through it will not allow the user to enter a new guess. It either keeps the first entry or if I clear the entry says it is invalid input. Any and all help is appreciated. Because this is a homework assignment I am required to user certain elements, such as try catch, and a repeating loop such as a for loop, or a do while loop. Thanks for your help.

private void Guess_btn_Click(object sender, EventArgs e)
        {
            try
            {
                Random random = new Random();
                int Answer = random.Next(0, 99);
            int User_Guess = 0;
            int Guess_num = 0;

            do
            {
                User_Guess = int.Parse(Guess_txtbx.Text);
                Guess_num++;

                if (User_Guess < Answer)
                {
                    MessageBox.Show("Your answer is too low, try again.");
                    Guess_txtbx.Clear();
                    Guess_txtbx.Focus();
                    User_Guess = int.Parse(Guess_txtbx.Text);
                }
                else if (User_Guess > Answer)
                {
                    MessageBox.Show("Your answer is too high, try again.");
                    Guess_txtbx.Clear();
                    Guess_txtbx.Focus();
                    User_Guess = int.Parse(Guess_txtbx.Text);
                }
                else
                {
                    MessageBox.Show("Your answer is correct! It took you " + Guess_num + "number of guesses.");
                }
            } while (User_Guess != Answer);
        }

        catch (Exception ex)
        {
            //Display error message
            MessageBox.Show(ex.Message);
        }
    }




Aucun commentaire:

Enregistrer un commentaire