如果这不等于?然后这样做

时间:2013-01-28 02:52:43

标签: c#

我正在做一个刽子手游戏,并且刚开始创建随机单词,这将产生用户必须猜测的新单词,但有时随机代码将生成与之前使用的相同的单词。我的问题是......是否有符号或代码可以说随机数不等于...执行此代码块。

这是我的代码......

private void button1_Click(object sender, EventArgs e)
    {
        Random rW = new Random();

        foreach (TextBox textBox in addTextBox())
        {
            textBox.Visible = false;
        }

        RW = rW.Next(1, 4);

        if (RW == 1) //Cat
        {
            textBox1.Visible = true;
            textBox2.Visible = true;
            textBox3.Visible = true;

        }
        else if (RW == 2) //Elephant
        {
            textBox1.Visible = true;
            textBox2.Visible = true;
            textBox3.Visible = true;
            textBox4.Visible = true;
            textBox5.Visible = true;
            textBox6.Visible = true;
            textBox7.Visible = true;
            textBox8.Visible = true;
        }
        else if (RW == 3) //Giraffe
        {
            textBox1.Visible = true;
            textBox2.Visible = true;
            textBox3.Visible = true;
            textBox4.Visible = true;
            textBox5.Visible = true;
            textBox6.Visible = true;
            textBox7.Visible = true;
        }
        else if (RW == 4) //Monkey
        {
            textBox1.Visible = true;
            textBox2.Visible = true;
            textBox3.Visible = true;
            textBox4.Visible = true;
            textBox5.Visible = true;
            textBox6.Visible = true;
        }
        else
        {
        }
    }

2 个答案:

答案 0 :(得分:5)

您应该使用!= {{1}}。{/ p>

答案 1 :(得分:1)

使用通用列表,生成单词后,将其添加到列表中。然后生成一个新单词,直到生成的单词不包含在使用的单词列表中。另外,对于if / elseif语句,我会说使用switch语句,它会使它更具可读性。

还要确保usedWords集合在按钮事件处理程序之外,并且不在内部声明,否则每次单击按钮时都会重置它。

列出usedWords = new List();

private void button1_Click(object sender,EventArgs e)     {         随机rW = new Random();

    foreach (TextBox textBox in addTextBox())
    {
        textBox.Visible = false;
    }

    RW = rW.Next(1, 4);

    while(usedWords.Contains(RW))
{
    RW = rW.Next(1,4);
}

usedWords.Add(RW);


    if (RW == 1) //Cat
    {
        textBox1.Visible = true;
        textBox2.Visible = true;
        textBox3.Visible = true;

    }
    else if (RW == 2) //Elephant
    {
        textBox1.Visible = true;
        textBox2.Visible = true;
        textBox3.Visible = true;
        textBox4.Visible = true;
        textBox5.Visible = true;
        textBox6.Visible = true;
        textBox7.Visible = true;
        textBox8.Visible = true;
    }
    else if (RW == 3) //Giraffe
    {
        textBox1.Visible = true;
        textBox2.Visible = true;
        textBox3.Visible = true;
        textBox4.Visible = true;
        textBox5.Visible = true;
        textBox6.Visible = true;
        textBox7.Visible = true;
    }
    else if (RW == 4) //Monkey
    {
        textBox1.Visible = true;
        textBox2.Visible = true;
        textBox3.Visible = true;
        textBox4.Visible = true;
        textBox5.Visible = true;
        textBox6.Visible = true;
    }
    else
    {
    }
}