0

プログラムに乱数を生成させたい場合、次re-read/loopelse if statementようなステートメントが見つかるまでどうすればよいですか(button1.Text == "")

これは私のコードです、

    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?
                }

            }
4

3 に答える 3

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 == "");
 }
于 2013-01-23T14:06:34.647 に答える
1

ifステートメントをwhile()ループに入れます。次に、break;ステートメントを実行してループを終了する条件を設定します。

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;
}
于 2013-01-23T14:08:32.123 に答える
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
于 2013-01-23T14:09:23.030 に答える