猜猜C#中的游戏,我不能让游戏开始时用户猜测的数量显示出来

时间:2017-02-07 21:59:25

标签: c# user-interface int label

我在C#中创建一个Guess游戏,我不能让用户猜测的数字在游戏开始时显示为10,它只会在用户已经尝试1次猜测时显示,并且显示10总计程序将给用户11个猜测,你能帮帮我吗?

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 GuessingGame
{
    public partial class Form1 : Form
{
    int _randnum;
    int _userguess;

    void GetRandomNumber()
    {
        int min = 1;
        int max = 101;
        Random randomiser;
        randomiser = new Random();

        _randnum = randomiser.Next(min, max);


    }
    public Form1()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void buttonGuess_Click(object sender, EventArgs e)
    {
        string user = textBox1.Text;
        string guessnum = Convert.ToString(_userguess);
        int guess = 0;


        if (int.TryParse(user, out guess))
        {
            if (_userguess == 0)
            {
                buttonGuess.Enabled = false;
                label3.Text = "Game Over";
            }

            else if (guess == _randnum)
            {
                label3.Text = "Good guess";
                buttonGuess.Enabled = false;

            }
            else if (guess < _randnum)
            {
                label3.Text = "Too low guess";
                _userguess--;

            }
            else if (guess > _randnum)
            {
                label3.Text = "Too High guess";
                _userguess--;
            }
            else if (guess < 0 || guess > 100)
            {
                label3.Text = "Your guess is out of the range";
                _userguess--;
            }
        }
        else
        {
            label3.Text = "Yous should introduce a number";
            _userguess--;
        }
        label2.Text = guessnum;
    }

    private void buttonExit_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        GetRandomNumber();
        _userguess = 10;
    }

    private void buttonNew_Game_Click(object sender, EventArgs e)
    {
        GetRandomNumber();
        buttonGuess.Enabled = true;
        _userguess = 10;
    }
}

}

2 个答案:

答案 0 :(得分:0)

你可以做的是使用NewGame功能进行游戏设置并删除label2中的文本,并将猜测计数器设置为11。

private void NewGame()
{
    GetRandomNumber();
    buttonGuess.Enabled = true;
    label2.Text = string.Empty;
    _userguess = 11;
}

然后你可以在Form Load和New Game Button Click功能中使用它:

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

private void buttonNew_Game_Click(object sender, EventArgs e)
{
    NewGame();
}

答案 1 :(得分:0)

您正在将guessnum字符串设置在错误的位置。您希望它在if语句后显示数字,因此您可以在if语句之后设置变量。如果在if语句之前设置String,它将获得_userguess的初始值,即10。

要在游戏开始时显示10,您还必须在开始新游戏时设置label2的文本。

如果你想让guessnum每次调用时都输出正确的字符串。你应该把它变成一个属性或方法。

private string guessnum{ get{ return _userguess.ToString(); } }