C# 如果这不等于?然后做这个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14554807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If this is not equal to? Then do this
提问by Glen Hunter
I'm doing a hangman game, and just started to created the random word that will generate new word that users have to guess however sometimes the random code will generate the same word that was used previously. My question is... is there a symbol or code that will say if random number is not equal to... do this block of code.
我正在做一个刽子手游戏,刚刚开始创建随机单词,该随机单词将生成用户必须猜测的新单词,但有时随机代码会生成与以前使用的单词相同的单词。我的问题是......是否有一个符号或代码会说明随机数是否不等于......做这段代码。
Here's my code...
这是我的代码...
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
{
}
}
采纳答案by Ismael Ghalimi
You should use !=
which is an equality operator.
您应该使用!=
which 是一个相等运算符。
回答by GR7
Use a generic list and once a word has been generated, add it to the list. Then just generate a new word until the generated word is not contained in the used words list. Also, for the if/elseif statements, i'd say to use a switch statement instead, it makes it more readable.
使用通用列表,生成单词后,将其添加到列表中。然后只生成一个新词,直到生成的词不包含在已用词列表中。另外,对于 if/elseif 语句,我会说改用 switch 语句,它使其更具可读性。
Also make sure the usedWords collection is outside your button event handler, and not declared inside, otherwise it will be reset everytime you click the button.
还要确保 usedWords 集合在按钮事件处理程序之外,而不是在内部声明,否则每次单击按钮时都会重置。
List usedWords = new List();
List usedWords = new List();
private void button1_Click(object sender, EventArgs e) { Random rW = new Random();
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);
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
{
}
}