c#中随机数猜测游戏的麻烦

时间:2014-10-11 19:05:43

标签: c#

所以我正在为学校做一个项目,在猜测游戏中为1-100之间随机生成的数字。我目前遇到的唯一麻烦是,每次用户输入新号码时,生成的号码也会发生变化。我尝试将代码放在表单加载器中生成数字,但后来我无法在程序中访问它。这是我到目前为止所拥有的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{

    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void guessButton_Click(object sender, EventArgs e)
        {

            int userGuess;
            userGuess = int.Parse(guessText.Text);
            Random rand = new Random();
            int number = rand.Next(1, 100);

                label2.Text = "" + number;


                    if (userGuess > number)
                    {
                        resultLabel.Text = "Your guess is too high";
                    }

                    else if (userGuess < number)
                    {
                        resultLabel.Text = "Your guess is too low.";
                    }

            else if (userGuess == number)
            {
                resultLabel.Text = "That is correct!";
            }

                guessText.Clear();



        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

2 个答案:

答案 0 :(得分:2)

目前的情况是,每次Random功能运行时,guessButton_Click号都会被覆盖

您在Random函数中声明guessButton_Click,每次点击guess按钮时都会调用 (这也是内存泄漏!)。 要修复,请在命名空间中将其声明为全局变量:

编辑:以下代码编译正确,效果很好。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{

    public partial class Form1 : Form
    {
        int number = new Random().Next(1, 100);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void guessButton_Click(object sender, EventArgs e)
        {

            int userGuess;
            userGuess = int.Parse(guessText.Text);
            label2.Text = "" + number;


            if (userGuess > number)
            {
                resultLabel.Text = "Your guess is too high";
            }

            else if (userGuess < number)
            {
                resultLabel.Text = "Your guess is too low.";
            }

            else if (userGuess == number)
            {
                resultLabel.Text = "That is correct!";
            }

            guessText.Clear();



        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

答案 1 :(得分:0)

每按一次按钮,您将获得一个新的随机数,请尝试以下操作:

    public partial class Form1 : Form
{

    Random rand;
    int number;

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rand = new Random();
        number = rand.Next(1, 100);
    }

    private void guessButton_Click(object sender, EventArgs e)
    {
        int userGuess;
        userGuess = int.Parse(guessText.Text);

        label2.Text = "" + number;


        if (userGuess > number)
        {
            resultLabel.Text = "Your guess is too high";
        }

        else if (userGuess < number)
        {
            resultLabel.Text = "Your guess is too low.";
        }

        else if (userGuess == number)
        {
            resultLabel.Text = "That is correct!";
        }

        guessText.Clear();
    }


}