在c#中保留一个随机数

时间:2015-09-17 15:23:12

标签: c# winforms random

我的程序设置为让用户猜测1到10之间的整数。如果用户猜测太低或太高,他们会收到通知并可以再试一次。

我遇到的问题是,当用户猜错时,会生成一个新的随机数。因此,基本上用户在错误之后永远不会尝试猜测相同的数字。

我需要这样做,以便当用户猜错时他们仍在尝试猜测相同的值。

这是我的代码:

namespace IntegerGame
{
    public partial class guessGame : Form
    {
        int num1;
        int num2;

        public guessGame()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        private void guessButton_Click(object sender, EventArgs e)
        {
            Random rnd1 = new Random();
            num1 = rnd1.Next(1, 10);

            if (int.TryParse(textBox1.Text, out num2))
            {
                if (num2 < 0 || num2 > 10)
                {
                    textBox1.Clear();
                    MessageBox.Show("Please enter a number between 1 and 10");
                }
                else
                {
                    if (num2 > num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to high, please try again");
                    }

                    else if (num2 < num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to low, please try again");
                    }

                    else if (num2 == num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
                    }
                }

            }
            else
            {
                textBox1.Clear();
                MessageBox.Show("This is not a valid integer, please enter a valid integer");
            }
       }
    }
}

6 个答案:

答案 0 :(得分:7)

生成随机数作为guessGame的成员(或在构造函数中,在InitializeComponent之后),而不是每当用户按下按钮时

public partial class guessGame : Form
{
    Random rnd1 = new Random();
    int num1 = rnd1.Next(1, 10);
    int num2;
    ...

答案 1 :(得分:3)

每次单击该按钮时,都会运行以下代码:

Random rnd1 = new Random();
num1 = rnd1.Next(1, 10);

这意味着每次用户猜测时,都会生成一个新的随机数。

我建议制作随机数和随机数字段(编辑:注意到你的数字已经是一个字段),对于初始字段,在构造函数中创建它,如下所示:

private Random _rnd1;
private int num1; 

GuessGame()
{
    _rnd1 = new Random();
    _num1 = _rnd1.Next(1,10);
    InitializeComponent();
}

然后,当用户猜对时,您可以通过将_num1字段设置为随机的下一个数字来简单地生成新数字。

答案 2 :(得分:1)

检查生成随机数时是否需要生成数字

Random rnd1 = new Random();
int num1 = -1;

private void guessButton_Click(object sender, EventArgs e)
{
    if (num1 == -1)
    {            
        num1 = rnd1.Next(1, 10);
    }
    //...

    //assign -1 to num1 after successful guess.
    num1 = -1;
}

答案 3 :(得分:0)

初始化表单时应设置随机值。只有在表格关闭并重新打开时才会改变。

Random rnd1 = new Random();

public guessGame()
{
    InitializeComponent();
    num1 = rnd1.Next(1, 10);
}

答案 4 :(得分:0)

只需在构造函数中生成随机数,而不是在单击处理程序上生成:

namespace IntegerGame
{
public partial class guessGame : Form
{
    int num1;
    int num2;

public guessGame()
{
    Random rnd1 = new Random();
    num1 = rnd1.Next(1, 10);
    InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void guessButton_Click(object sender, EventArgs e)
{


    if (int.TryParse(textBox1.Text, out num2))
    {


            if (num2 < 0 || num2 > 10)
            {
                textBox1.Clear();
                MessageBox.Show("Please enter a number between 1 and 10");
            }

            else
            {
                if (num2 > num1)
                {
                    textBox1.Clear();
                    MessageBox.Show("You guessed to high, please try again");
                }

                else if (num2 < num1)
                {
                    textBox1.Clear();
                    MessageBox.Show("You guessed to low, please try again");
                }

                else if (num2 == num1)
                {
                    textBox1.Clear();
                    MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
                }
            }

    }

    else
    {
        textBox1.Clear();
        MessageBox.Show("This is not a valid integer, please enter a valid integer");
    }




}

} }

答案 5 :(得分:0)

namespace IntegerGame
{
public partial class guessGame : Form
{
    int num1;
    int num2;
    Random rnd1 = new Random();

    public guessGame()
    {
        InitializeComponent();
        num1 = rnd1.Next(1, 10);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void guessButton_Click(object sender, EventArgs e)
    {

        if (int.TryParse(textBox1.Text, out num2))
        {


                if (num2 < 0 || num2 > 10)
                {
                    textBox1.Clear();
                    MessageBox.Show("Please enter a number between 1 and 10");
                }

                else
                {
                    if (num2 > num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to high, please try again");
                    }

                    else if (num2 < num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to low, please try again");
                    }
                    // Note that this could be an else: it is the only case left
                    else if (num2 == num1)
                    {
                        num1 = rnd1.Next(1, 10);
                        textBox1.Clear();
                        MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
                    }
                }

        }

        else
        {
            textBox1.Clear();
            MessageBox.Show("This is not a valid integer, please enter a valid integer");

}

您只想在数字相等时生成数字,而不是在按下按钮时生成数字。