Windows窗体应用程序——C#随机数字猜谜游戏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9313817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:51:14  来源:igfitidea点击:

Windows form application - C# Random numer guessing game

c#visual-studiowindows-forms-designer

提问by user1174357

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.

我需要一些关于 Visual Studio 中的随机数猜谜游戏的帮助。我的代码首当其冲,但我在使用随机数生成器并将随机数移植到点击事件中时遇到了麻烦。与往常一样,我真的不需要代码,而是一些指导和/或解释,说明我做错了什么,以及在学习的初学者阶段是否有更有效的方法。下面是我的代码,注释是我遇到麻烦的部分。感谢您的任何帮助,因为我迄今为止收到的帮助非常出色。

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

采纳答案by SWeko

As the rand1and answervariables are defined within the constructor, you can only access them in the constructor. Defining answeron 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:

由于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);
}

回答by raveturned

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(…).

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

回答by Ryan P

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

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

回答by Dervall

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

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

回答by Philip Badilla

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

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

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

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

just reset the counter when the user have guessed correctly

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