Skip to content

Guessing Game Help

Michael Hall

Asked Nov 25, 2006, 6:44 PM EST1 answer
Share
LinkedInX
I am new to programming in general. I am trying to complete a project for school and I am running into trouble. I keep putting myself into a loop. Can somebody please help me?

static void Main()
{
   Application.Run(new Form1());
}

private void button2_Click(object sender, System.EventArgs e)
{
   MessageBox.Show("Guess a number between 1 and 100\n"
+ "Red Form = Guess is too HIGH!\n"
+ "Blue Form = Guess is too LOW!");
}

private void button1_Click(object sender, System.EventArgs e)
{
   Random r = new Random();
   int target = r.Next(1,100),
     guess = 0,
     guessTotal = 0,
     high = 0,
     low = 0;
   string invalue = "";

   while(guess != target)
   {
      invalue = this.txtGuess.Text;
      guess = int.Parse(invalue);

      if(guess == target)
        {
           MessageBox.Show("Good Job!\n"
               + "You guessed correctly on the first try\n"
               + "The number was " + target);
        }
        else
        {
           // MessageBox.Show("Wrong Guess.\n" + "Try again");
           guessTotal++;

           if(guess > target)
           {
              //Change Form color red
              MessageBox.Show("Too High");
              high++;
           }
           
           if(guess < target)
           {
              //Change form color blue
              MessageBox.Show("Too Low");
              low++;
            }
       }
  }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi!

    I think your problem may be that each time button1 is clicked, you're setting the values of your variables to 0. I'm not seeing where their value is changed before the loop starts, so the value of guess is always 0...

    Jennifer
    0