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

时间:2017-07-19 21:47:36

标签: c# forms increment

我正在尝试对数字游戏做一个简单的猜测,但在一种形式中,问题是当我点击btnGuess时,即使它是空白的,尽管比较逻辑,得分也会上升。如果我删除第59行的guessCount和lblguessCount.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();

            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

您还要在更改值之前将lblDebug.Text设置为randNum 的值。这意味着它始终显示先前的随机数,而不是当前的随机数。要解决此问题,只需在Text分配后添加randNum属性。

此外,btnGuess_Click方法中的代码每次获得错误猜测时都会从其分数中减去一个代码。可能我们应该忽略不正确的猜测,而只是给他们最少的尝试次数。

但是,大多数情况下,代码感觉它是在没有正确设计的情况下编写的(对不起,如果我错了)。我通常做的是首先写出场景,然后编写我喜欢我的最终代码的伪代码,最后在实际代码中实现。

例如:

  

场景:
1。表格载荷。
2。随机数在1和100之间选择。用户被通知他们有15次尝试猜测该号码。
4。用户在文本框中输入一个数字并按下按钮
5。如果数字匹配,祝贺他们并重置游戏(返回步骤#2)
6。如果数字不正确,请告诉他们是否太低或两个高,然后转到步骤4.
7。如果用户没有猜到,请让他们知道号码是什么并重置游戏。

我想写的代码看起来像:

  • 在表格加载中,我们会调用ResetGame方法
  • 在ResetGame方法中,我们会重置猜测次数,选择一个随机数,并设置一个包含说明的消息框
  • 在Button Click事件中,我们调用名为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 :(得分:-1)

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

private void btnGuess_Click(object sender, EventArgs e)
    {
        {
            if(guess <= 10 && guess >= 0)
            {
                if (guess == randNum)
                {
                    score += 1;
                    lblScore.Text = score.ToString();

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

                guessCount++;
                lblguessCount.Text = guessCount.ToString();
            }else{score -=1;}
        }
    }