C#我怎么这个简单的循环直到真的?

时间:2013-01-28 21:06:57

标签: c# winforms visual-studio-2012

我正在制作一个刽子手游戏,这部分代码落后于按钮新游戏。每当用户选择新游戏时,我希望游戏加载一个新单词供用户猜测。我的问题是我需要将我的代码放入循环中。例如,如果if / else语句不为true,则选择另一个随机数,直到语句为真...

我希望有意义=)到目前为止,这是我的代码。

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



    }
}

}

2 个答案:

答案 0 :(得分:2)

不要循环检查,而是尝试循环,直到得到你喜欢的随机数。

以下是一些关于如何做类似事情的伪代码。

//while the next random number is not the same as the previous
    //Generate a new random number

一旦你有一个随机数,你就不必检查那些ifs中的前一个数字。

您可能还想查找switch语句的文档,因为这可能会使您的代码更具可读性。

答案 1 :(得分:1)

把它放在你的代码周围:

while(true)
{
// CODE
break;
}

并在else {}部分内:

continue;

编辑: 你也可以使用LINQ,e。克。

while(Controls.OfType<TextBox>().All(t => !t.Visible))

如果您只有那些可见性在循环内发生变化的框。