为用户输入创建计数器C#

时间:2015-04-24 05:05:01

标签: c#

嘿伙计我需要帮助创建一个用户输入计数器,根据他们输入猜测的时间从1到100猜测一个随机数。到目前为止这是我所拥有但它只输出1个计数而不是计算下一个输入。你能告诉我我做错了吗?

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 GuessingGameGUI
{
    public partial class frmGuess : Form
    {
         public frmGuess()
     {
        InitializeComponent();
    }

    private void frmGuess_Load(object sender, EventArgs e)
    {
        lblCount.Visible = true;
        lblHowMuch.Visible = true;

    }

    private void btnReset_Click(object sender, EventArgs e)
    {
        txtGuess.Text = "";
        lblCount.Text = "";
        lblHowMuch.Text = "";
        this.BackColor = System.Drawing.Color.Empty;
        txtGuess.Focus();
    }

    private void btnCheck_Click(object sender, EventArgs e)
    {
        Random r = new Random();
        int target = r.Next(0, 101);

        int userGuess = int.Parse(txtGuess.Text);

        int guessCount = 0;

            if (userGuess == target)
            {
                guessCount++;
                this.BackColor = System.Drawing.Color.DarkOliveGreen;
                lblHowMuch.Text = "You guess the right number " + "it took you: " + guessCount.ToString() + " guesses";

            }
            else if (userGuess < target)
            {
                guessCount++;
                this.BackColor = System.Drawing.Color.Yellow;

            }
            else if (userGuess > target)
            {
                guessCount++;
                this.BackColor = System.Drawing.Color.Red;

            }

        lblCount.Text = "You made: " + guessCount.ToString() + " Guesses";

        }
    }
}

3 个答案:

答案 0 :(得分:1)

它不是那么明显。你为什么在btnCheck_Click中设置int guessCount = 0?为什么不保留全球反击?

答案 1 :(得分:1)

您的代码存在的问题是,每次点击btnCheck时,您都会将猜测计数器设置为零。您需要确保每次猜测时只重置一次。

因此,这意味着您需要将guessCount作为类级别变量移出,并确保只在第一次运行表单时以及每次单击btnReset时重置它。

以下是我将重构代码以实现此目的的方法:

public partial class frmGuess : Form
{
    public frmGuess()
    {
        InitializeComponent();
    }

    private void frmGuess_Load(object sender, EventArgs e)
    {
        lblCount.Visible = true;
        lblHowMuch.Visible = true;
        ResetData();
    }

    private void btnReset_Click(object sender, EventArgs e)
    {
        ResetData();
    }

    private Random r = new Random();
    private int guessCount;
    private int target;

    private void ResetData()
    {
        guessCount = 0;
        target = r.Next(0, 101);
        txtGuess.Text = "";
        lblCount.Text = "";
        lblHowMuch.Text = "";
        this.BackColor = System.Drawing.Color.Empty;
        txtGuess.Focus();
    }

    private void btnCheck_Click(object sender, EventArgs e)
    {
        int userGuess = int.Parse(txtGuess.Text);
        guessCount++;

        if (userGuess == target)
        {
            this.BackColor = System.Drawing.Color.DarkOliveGreen;
            lblHowMuch.Text = String.Format(
                "You guessed the right number it took you {0} guesses",
                guessCount);
        }
        else
        {
            this.BackColor = userGuess < target
                ? System.Drawing.Color.Yellow
                : System.Drawing.Color.Red;
        }

        lblCount.Text = String.Format(
            "You made {0} Guesses",
            guessCount);
    }
}

您还会注意到,每次点击target时您都会重置btnCheck。这也需要转移到类级变量。

这也是一个很好的习惯,也可以让Random实例成为一个类级别的变量,因为如果你不这样做,你可能会得到少于随机数的情况。

您会注意到我将所有重置代码移动到一个新的ResetData方法中,该方法可以在加载表单和单击btnReset时调用。

答案 2 :(得分:0)

要将其设为全局,请从int guessCount = 0;功能中取出btnCheck_Click并将其放在显示位置的顶部:

namespace GuessingGameGUI
{

    public partial class frmGuess : Form
    {

    int guessCount = 0;

那样guessCount不会连续重置为零,然后每次按下btnCheck按钮时都会递增。