Windows窗体应用-C#随机猜数游戏

发布于 2025-01-06 01:41:21 字数 2451 浏览 1 评论 0原文

我需要视觉工作室中的随机数猜测游戏的一些帮助。我得到了代码的冲击,但我在随机数生成器和将随机数移植到点击事件中遇到了麻烦。与往常一样,我实际上并不需要代码,而是需要一些指导和/或解释,说明我做错了什么,以及在学习的初学者阶段是否有更有效的方法。下面是我的代码,注释是我遇到麻烦的部分。感谢您的任何帮助,因为迄今为止我收到的帮助是惊人的。

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

namespace LAB6B
{
    public partial class game : Form
    {
        public game()
        {
            InitializeComponent();

            //Generate Random number between 1 and 100
         //Not sure if there is a better way?
            Random rand1 = new Random();
            int num1 = rand1.Next(1,50);
            int num2 = rand1.Next(1,50);
            int answer = num1 + num2;

        }

        private void evaluate_Click(object sender, EventArgs e)
        {
            int count = 0;
            int choice = Convert.ToInt32(guess);


            if (guess.Text != string.Empty)
            {
                // set counter to keep track of how many tries
                // should this be done by a loop or will it count without a loop?
                count++;

                //compare user input against random number
          //Can’t import the random number for comparision
                if (choice < answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too Low!";
                    Clear.Visible = true;
                    BackColor = Color.LightSeaGreen;
                }
                else if (choice > answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too High!";
                    Clear.Visible = true;
                    BackColor = Color.SlateBlue;
                }
                else
                {
                    //Display correct message along with how many times it took to get it
                    MessageBox.Show(" Eso es CORRECTO! It took you {0} tries. ", count);
                }
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            guess.Text = "";
            Evaluate.Visible = true;
            lblMessage.Visible = false;
            Clear.Visible = false;
            BackColor = Color.PowderBlue;
        }
    }
}

I need a little help with a Random Number Guessing Game in visual studio. I got the brunt of the code down but I am having troubles with the Random number generator and getting the random number to port into the click events. As always, I don't really need code but some guidance and/or explanations as to what I am doing wrong and if there is a more effecient way to do things in the beginner phases of learning. Below is my code, the comments are the parts where I am having troubles. Thanks for any help as the help I've recieved to date as been phenominal.

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

namespace LAB6B
{
    public partial class game : Form
    {
        public game()
        {
            InitializeComponent();

            //Generate Random number between 1 and 100
         //Not sure if there is a better way?
            Random rand1 = new Random();
            int num1 = rand1.Next(1,50);
            int num2 = rand1.Next(1,50);
            int answer = num1 + num2;

        }

        private void evaluate_Click(object sender, EventArgs e)
        {
            int count = 0;
            int choice = Convert.ToInt32(guess);


            if (guess.Text != string.Empty)
            {
                // set counter to keep track of how many tries
                // should this be done by a loop or will it count without a loop?
                count++;

                //compare user input against random number
          //Can’t import the random number for comparision
                if (choice < answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too Low!";
                    Clear.Visible = true;
                    BackColor = Color.LightSeaGreen;
                }
                else if (choice > answer)
                {
                    Evaluate.Visible = false;
                    lblMessage.Visible = true;
                    lblMessage.Text = "Too High!";
                    Clear.Visible = true;
                    BackColor = Color.SlateBlue;
                }
                else
                {
                    //Display correct message along with how many times it took to get it
                    MessageBox.Show(" Eso es CORRECTO! It took you {0} tries. ", count);
                }
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            guess.Text = "";
            Evaluate.Visible = true;
            lblMessage.Visible = false;
            Clear.Visible = false;
            BackColor = Color.PowderBlue;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

五里雾 2025-01-13 01:41:21

由于 rand1answer 变量是在构造函数中定义的,因此您只能在构造函数中访问它们。在类级别定义 answer 将解决大多数问题,因为您将能够从构造函数和点击处理程序访问它,如下所示:

private int answer;
private int count;

public game()
{
  InitializeComponent();

  //Generate Random number between 1 and 100
  Random random= new Random();
  // no need for num1 and num2, it's just as random
  answer = random.Next(1,101);
}

As the rand1 and answer variables are defined within the constructor, you can only access them in the constructor. Defining answer on the class level will solve most of the problems, as you will be able to access it both from the constructor and the click handlers, like this:

private int answer;
private int count;

public game()
{
  InitializeComponent();

  //Generate Random number between 1 and 100
  Random random= new Random();
  // no need for num1 and num2, it's just as random
  answer = random.Next(1,101);
}
横笛休吹塞上声 2025-01-13 01:41:21

我认为你有一个范围问题。 “answer”变量是在构造函数内声明的,因此它对于评估_Click(…)内的代码不可见。

I think you have an issue of scope. The "answer" variable is declared inside your constructor, so it will not be visible to the code inside evaluate_Click(…).

吃颗糖壮壮胆 2025-01-13 01:41:21

看起来您需要将 answer 声明为类变量。当您在构造函数中声明变量时,它仍然是该方法的本地变量,并且不可用于其他方法。

Looks like you need to declare answer as a class variable. When you declare a variable in a constructor, it's still local to that method and not available to other methods.

小鸟爱天空丶 2025-01-13 01:41:21

我真的不知道您想要回答什么,但一个明显的错误是您必须将 count 变量定义为成员变量才能跟踪尝试次数。现在,每次用户按下按钮时,count 都将始终初始化为零。

I do not really know what you want answered, but an obvious error is that you must define your count variable as a member variable in order to keep track of the number of tries. As it is now, the count will always be initialized as zero each time the user presses the button.

鹿港小镇 2025-01-13 01:41:21

首先,您需要在页面级别声明变量 answer,以便其他页面级别函数可以使用它。

在计数器中这样做,

public partial class game : Form
    {
        int answer;
        public game()
        {
        }
    }

您可以使用静态计数器或页面级变量,例如变量answer

只需在用户猜对时重置计数器

First of, you need to declare your variable answer in the page level so it can be used by other page level functions.

Do it like this

public partial class game : Form
    {
        int answer;
        public game()
        {
        }
    }

in your counter you can use a static counter or a page level variable also such as the variable answer

just reset the counter when the user have guessed correctly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文