我正在制作一个刽子手游戏,这部分代码位于按钮新游戏的后面。每次用户选择新游戏时,我希望游戏加载一个新词供用户猜测。我的问题是我需要将我的代码放入一个循环中。例如,如果没有 if/else 语句不为真,则选择另一个随机数,直到语句为真...
我希望这是有道理的 =) 这是我到目前为止的代码。
namespace HangMan
{
public partial class Form1 : Form
{
int RW;
string previous;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (TextBox textBox in addTextBox())
{
textBox.Visible = false;
textBox.Text = "";
}
}
private static void LoadImages()
{
Image newImageIntro = Image.FromFile("HangmanIntro.jpg");
Image newImage1 = Image.FromFile("1.jpg");
Image newImage2 = Image.FromFile("2.jpg");
Image newImage3 = Image.FromFile("3.jpg");
Image newImage4 = Image.FromFile("4.jpg");
Image newImage5 = Image.FromFile("5.jpg");
Image newImage6 = Image.FromFile("6.jpg");
Image newImage7 = Image.FromFile("7.jpg");
Image newImage8 = Image.FromFile("8.jpg");
Image newImage9 = Image.FromFile("9.jpg");
}
private void txtEnText_TextChanged(object sender, EventArgs e)
{
}
private void txtEnText_Enter(object sender, EventArgs e)
{
}
public List<TextBox> addTextBox()
{
List<TextBox> tBox = new List<TextBox>();
tBox.Add(textBox1);
tBox.Add(textBox2);
tBox.Add(textBox3);
tBox.Add(textBox4);
tBox.Add(textBox5);
tBox.Add(textBox6);
tBox.Add(textBox7);
tBox.Add(textBox8);
tBox.Add(textBox9);
tBox.Add(textBox10);
return tBox;
}
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 & previous != "1" ) //Cat
{
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
previous = "1";
}
else if (RW == 2 & previous != "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;
previous = "2";
}
else if (RW == 3 & previous != "3") //Giraffe
{
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
textBox5.Visible = true;
textBox6.Visible = true;
textBox7.Visible = true;
previous = "3";
}
else if (RW == 4 & previous != "4") //Monkey
{
textBox1.Visible = true;
textBox2.Visible = true;
textBox3.Visible = true;
textBox4.Visible = true;
textBox5.Visible = true;
textBox6.Visible = true;
previous = "4";
}
else
{
}
}
}
}