C#如何增加/减少标签?

C#如何增加/减少标签?,c#,forms,increment,C#,Forms,Increment,我正在尝试做一个简单的数字猜测游戏,但在某种形式上,问题是当我点击b按钮时,分数会上升,即使它是空的,尽管有比较逻辑。如果我删除第59行的guessCount和lblguesCount.Text=guessCount.ToString();它只是以负片的形式出现。即使数字是正确的猜测,如果它是负数或正数,它也不会改变 using System; using System.Collections.Generic; using System.ComponentModel; using Sys

我正在尝试做一个简单的数字猜测游戏,但在某种形式上,问题是当我点击b按钮时,分数会上升,即使它是空的,尽管有比较逻辑。如果我删除第59行的guessCount和lblguesCount.Text=guessCount.ToString();它只是以负片的形式出现。即使数字是正确的猜测,如果它是负数或正数,它也不会改变

    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 Guess_The_Number_V2
{
    public partial class Form1 : Form
    {
        private int score = 0;
        private int randNum;
        private int guess = 0;
        private int guessCount = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void lblGenerate_Click(object sender, EventArgs e)
        {
            lbldebug.Text = randNum.ToString();
            Random rand = new Random();
            randNum = rand.Next(0, 10);
        }

        private void txtGuess_TextChanged(object sender, EventArgs e)
        {
            guess = Convert.ToInt32(txtGuess.Text);
        }


        private void btnGuess_Click(object sender, EventArgs e)
        {
            {

                if (guess == randNum)
                {
                    score += 1;
                    lblScore.Text = score.ToString();

                }
                else if (guess != randNum)
                {
                    score-=1;
                    lblScore.Text = score.ToString();
                }

                guessCount++;
                lblguessCount.Text = guessCount.ToString();

            }
        }
    }
}
private void txtGuess\u text已更改(对象发送方,事件参数e)
{
if(txtGuess.Text!=null)
guess=将.ToInt32(txtGuess.Text)转换为;
}
private void b点击(对象发送者,事件参数e)
{
{
如果(猜测=0)
{
if(guess==randNum)
{
分数+=1;
lblScore.Text=score.ToString();
}
else if(猜测!=randNum)
{
分数-=1;
lblScore.Text=score.ToString();
}
猜数++;
lblguessCount.Text=guessCount.ToString();
}else{score-=1;}
}
}

您的代码有一些问题。每次单击按钮时,不应生成新的
随机
。您应该创建一个类级别的
Random
变量,并在默认情况下将其设置为
new Random()
(一次)

在更改值之前,您还将lblDebug.Text设置为
randNum
。这意味着它总是显示以前的随机数,而不是当前的随机数。要解决这个问题,只需将
Text
属性的赋值放在
randNum
的赋值之后

此外,每次他们猜到错误的答案时,你在
b语言点击
方法中的代码都会从他们的分数中减去一分。也许我们应该忽略不正确的猜测,只给他们最少的尝试次数

不过,大多数情况下,代码感觉好像是在没有正确设计的情况下编写的(如果我错了,很抱歉)。我通常做的是首先写出场景,然后编写我希望最终代码看起来像的伪代码,最后在真实代码中实现

例如:

情景:
1。表单加载
2。随机数在1和100之间选择
3。通知用户他们有15次尝试猜测数字
4。用户在文本框中输入数字并按下按钮
5。如果数字匹配,祝贺他们并重置游戏(返回步骤2)
6。如果数字不正确,告诉他们是过低还是过高,然后转到步骤4。
7。如果用户没有猜测,让他们知道数字是多少,然后重新设置游戏

我想写的代码如下所示:

  • 在表单加载中,我们将调用
    ResetGame
    方法
  • 在ResetGame方法中,我们将重置猜测的次数,选择一个随机数,并设置一个带有说明的消息框
  • 在按钮点击事件中,我们调用名为
    CheckForWinner
  • CheckForWinner
    方法中,我们看到他们输入了一个有效的数字
    • 如果他们没有显示提示他们需要更正猜测的消息
    • 如果有,看看数字是否与我们的随机数匹配
    • 如果有,则向用户显示消息,然后调用
      ResetGame
    • 如果没有,则调用方法
      DisplayHighLowMessage
    • 如果没有,我们调用一个方法
      FinalizeTurn
  • DisplayHighLowMessage
    方法中,我们将数字与随机数进行比较,并显示一条消息,指示数字是过低还是过高
  • FinalizeTurn
    方法中,我们增加猜测计数,看看它是否超过最大值
    • 如果大于最大猜测次数,请让用户知道游戏结束,向他们显示号码,然后致电
      ResetGame
现在我对程序流程和需要创建的方法有了一个大致的了解,我们可以创建它们了。我们知道我们需要类级变量来存储猜测次数、当前分数、当前猜测和随机数。我们还需要一个类级别的
Random
变量,因为它只需要初始化一次

下面是代码的外观:

public partial class Form1 : Form
{
    private int score;
    private int randNum;
    private int guess;
    private int guessCount;
    private const int MaxGuesses = 15;
    private readonly Random rnd = new Random();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ResetGame();
    }

    private void ResetGame()
    {
        // Choose new random number
        randNum = rnd.Next(1, 101);
        lblDebug.Text = randNum.ToString();

        // Reset variables
        guessCount = 0;
        lblGuessCount.Text = guessCount.ToString();
        txtGuess.Text = "";

        // Show instructions
        MessageBox.Show("I've chosen a random number from 1 to 100." +
            $" You have {MaxGuesses} tries to guess it!");
    }

    private void btnGuess_Click(object sender, EventArgs e)
    {
        CheckForWinner();
    }

    private void CheckForWinner()
    {
        if (guess == randNum)
        {
            // Increment the score
            score += 1;
            lblScore.Text = score.ToString();

            // Tell user they won, and reset game
            MessageBox.Show("Congratulations! You guessed" +
                $" the number in {guessCount} tries!");

            ResetGame();
        }
        else
        {
            // Tell them if they're too high or low, and finish this turn
            DisplayHighLowMessage();
            FinalizeTurn();
        }
    }

    private void DisplayHighLowMessage()
    {
        MessageBox.Show(guess < randNum
            ? "That guess is too low!"
            : "That guess is too high!");
    }

    private void FinalizeTurn()
    {
        // Increment guess count
        guessCount++;
        lblGuessCount.Text = guessCount.ToString();

        // If they've used all their guesses, show them the number and reset the game
        if (guessCount > MaxGuesses)
        {
            MessageBox.Show($"Sorry, you're out of guesses! The number was: {randNum}.");
            ResetGame();
        }
    }

    private void txtGuess_TextChanged(object sender, EventArgs e)
    {
        // If the textbox is being cleared, allow it and reset the guess.
        if (txtGuess.Text == "")
        {
            guess = 0;
        }
        // Otherwise, use int.TryParse in case the 'Text' property 
        // doesn't contian a valid number. The code below says, 
        // "if TryParse succeeds, update our guess with the new number"
        int newGuess;
        if (int.TryParse(txtGuess.Text, out newGuess))
        {
            guess = newGuess;
        }

        // Ensure our textbox is displaying the current value of 'guess'
        txtGuess.Text = guess.ToString();
        txtGuess.SelectionStart = txtGuess.TextLength;
    }
}
公共部分类表单1:表单
{
个人智力得分;
私有内部随机数;
私人智力测验;
私人整数猜测计数;
private const int MaxGuesses=15;
私有只读随机rnd=新随机();
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
重置游戏();
}
私人游戏()
{
//选择新的随机数
randNum=rnd.Next(1101);
lblDebug.Text=randNum.ToString();
//重置变量
猜测计数=0;
lblGuessCount.Text=guessCount.ToString();
txtGuess.Text=“”;
//指示
Show(“我选择了一个从1到100的随机数。”+
$“您有{MaxGuesses}尝试猜测它!”;
}
private void b点击(对象发送者,事件参数e)
{
CheckForWinner();
}
私有void CheckForWinner()
{
if(guess==randNum)
{
//增加分数
分数+=1;
lblScore.Text=score.ToString();
//告诉用户他们赢了,并重置游戏
Show(“祝贺你!你猜对了”+
$“尝试{guessCount}中的数字!”);
重置游戏();
}
其他的
{
//告诉他们是太高还是太低,然后完成本回合
DisplayHighLowMessage();
FinalizeTurn();
}
}
私有void DisplayHighLowMessage()
{
MessageBox.Show(猜测<随机数
public partial class Form1 : Form
{
    private int score;
    private int randNum;
    private int guess;
    private int guessCount;
    private const int MaxGuesses = 15;
    private readonly Random rnd = new Random();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ResetGame();
    }

    private void ResetGame()
    {
        // Choose new random number
        randNum = rnd.Next(1, 101);
        lblDebug.Text = randNum.ToString();

        // Reset variables
        guessCount = 0;
        lblGuessCount.Text = guessCount.ToString();
        txtGuess.Text = "";

        // Show instructions
        MessageBox.Show("I've chosen a random number from 1 to 100." +
            $" You have {MaxGuesses} tries to guess it!");
    }

    private void btnGuess_Click(object sender, EventArgs e)
    {
        CheckForWinner();
    }

    private void CheckForWinner()
    {
        if (guess == randNum)
        {
            // Increment the score
            score += 1;
            lblScore.Text = score.ToString();

            // Tell user they won, and reset game
            MessageBox.Show("Congratulations! You guessed" +
                $" the number in {guessCount} tries!");

            ResetGame();
        }
        else
        {
            // Tell them if they're too high or low, and finish this turn
            DisplayHighLowMessage();
            FinalizeTurn();
        }
    }

    private void DisplayHighLowMessage()
    {
        MessageBox.Show(guess < randNum
            ? "That guess is too low!"
            : "That guess is too high!");
    }

    private void FinalizeTurn()
    {
        // Increment guess count
        guessCount++;
        lblGuessCount.Text = guessCount.ToString();

        // If they've used all their guesses, show them the number and reset the game
        if (guessCount > MaxGuesses)
        {
            MessageBox.Show($"Sorry, you're out of guesses! The number was: {randNum}.");
            ResetGame();
        }
    }

    private void txtGuess_TextChanged(object sender, EventArgs e)
    {
        // If the textbox is being cleared, allow it and reset the guess.
        if (txtGuess.Text == "")
        {
            guess = 0;
        }
        // Otherwise, use int.TryParse in case the 'Text' property 
        // doesn't contian a valid number. The code below says, 
        // "if TryParse succeeds, update our guess with the new number"
        int newGuess;
        if (int.TryParse(txtGuess.Text, out newGuess))
        {
            guess = newGuess;
        }

        // Ensure our textbox is displaying the current value of 'guess'
        txtGuess.Text = guess.ToString();
        txtGuess.SelectionStart = txtGuess.TextLength;
    }
}