0

What should I do if I wanted the program to generate a random number then re-read/loop the else if statement until it finds a statement that is similar to this if (button1.Text == ""), The random number only needs to go up to 9.

This is my code,

    private void button1_Click(object sender, EventArgs e)
    {
          var rc = new Random();
          storeRI = rc.Next(1, 9);

            if (storeRI == 1)
            {
                if (button1.Text == "")
                {
                    button1.Text = "X";
                }
                else
                {
                   //Need to generate another random number
                    //And read the else if statement again... how?
                }

            }

            else if (storeRI == 2)
            {
                if (button1.Text == "")
                {
                    button1.Text = "X";
                }
                else
                {
                   //Need to generate another random number
                    //And read the else if statement again... how?
                }

            }
spajce
  • 7,044
  • 5
  • 29
  • 44
Glen Hunter
  • 65
  • 1
  • 2
  • 11
  • 1
    Check this: http://stackoverflow.com/questions/10688044/filling-a-array-with-uniqe-random-numbers-between-0-9-in-c-sharp – Dejo Jan 23 '13 at 14:03
  • 2
    I did not really understand the purpose of this code section. – daryal Jan 23 '13 at 14:08
  • The first thing comes into my mind is `while loop`. Then, I browse down and see what others had answered, and bingo! We have the same idea. `while(I_havent_find_the_answer) do` – alont Jan 23 '13 at 14:21

3 Answers3

2
private void button1_Click(object sender, EventArgs e)
{
      var rc = new Random();
      do
      {
        storeRI = rc.Next(1, 9);

        if (storeRI == 1)
        {
            if (button1.Text == "")
            {
                button1.Text = "X";
            }
        }

        else if (storeRI == 2)
        {
            if (button1.Text == "")
            {
                button1.Text = "X";
            }
        }
      } while (button1.Text == "");
 }
Vincent James
  • 1,120
  • 3
  • 16
  • 27
1

put the if statements in a while() loop. Then have a contidion that executes a break; statement to terminate the loop:

while(button1.Text == "")
{
    if (storeRI == 1)
    {
        if (button1.Text == "")
        {
            button1.Text = "X";
        }
        else
        {
             //Need to generate another random number
             storeRI = rc.Next(1, 9);
        }

    }

    else if (storeRI == 2)
    {
     ...
    }
    else
        break;
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
1
var rc = new Random();
int[] numbers = { 1, 2 }; // numbers from your if..else blocks

do {
  storeRI = rc.Next(1, 9);

  if (!numbers.Contains(storeRI))
      break; // not matched any if..else block

  if (button1.Text == "")
  {
      button1.Text = "X";
      break; // set text and break loop
  }

} while(true); // generate new number and do check again
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459