验证按钮不正确?

时间:2016-09-16 04:31:27

标签: c# winforms

因此,我需要创建一个在2个标签中生成2个数字的应用程序,然后用户必须输入这2个数字的附加值作为答案。 然后我需要创建一个按钮,检查用户是否在文本框中输入了正确的解决方案......

然而,我制作的复选按钮,每次都显示“不正确”的答案......我做错了什么?

private void generatebutton_Click(object sender, EventArgs e

    {

            //Declaring Variables for Generate button
            int answer = 0;
            Random rand = new Random();
            var intNum1 = rand.Next(100, 500);
            var intNum2 = rand.Next(100, 500);
            answer = intNum1 + intNum2;


            //Generate integer number 1 in first label 
            firstnumberlabel.Text = intNum1.ToString();

            //Generate integer number 2 in second label
            secondNumberlabel.Text = intNum2.ToString();
     }

    private void checkButton_Click(object sender, EventArgs e)

      {
            //Declaring variables for answer label
            int userInput = 0;
            int answer = 0;
            Random rand = new Random();
            var intNum1 = rand.Next(100, 500);
            var intNum2 = rand.Next(100, 500);
            answer = intNum1 + intNum2;

            if (int.TryParse(txtanswer.Text, out userInput))
            {
                if (userInput == answer)
                {
                    // Display success message
                    MessageBox.Show("Correct!");
                }

                else 
                {
                    // Display wrong answer message
                    MessageBox.Show("Incorrect!");
                }
        }
        else
        {
            // show message that invalid input
        } 
    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        //Closes Application
        this.Close();
    }

    private void button4_Click(object sender, EventArgs e)
    {
        //Clears text box
        txtanswer.Text = "";
    }
}

}

2 个答案:

答案 0 :(得分:0)

您需要将用户对随机数集的回答进行比较,而不是原来的两个随机数。

在checkButton_Click事件中,在int answer = 0之后替换4行;像answer = int.Parse(firstnumberlabel.Text) + int.Parse(secondnumberlabel.Text);

这样的东西

在用户尝试检查答案而不提供数字的情况下,仍然需要添加一些输入完整性检查以避免异常。

答案 1 :(得分:0)

如下修改将起作用。你正在创建新的随机数来找到错误的答案

private void generatebutton_Click(object sender, EventArgs e
{
        //Declaring Variables for Generate button
        Random rand = new Random();
        var intNum1 = rand.Next(100, 500);
        var intNum2 = rand.Next(100, 500);

        //Generate integer number 1 in first label 
        firstnumberlabel.Text = intNum1.ToString();

        //Generate integer number 2 in second label
        secondNumberlabel.Text = intNum2.ToString();
 }

private void checkButton_Click(object sender, EventArgs e)
{
    int userInput = 0;
    int answer = int.Parse(firstnumberlabel.Text) + int.Parse(secondnumberlabel.Text);
    if (int.TryParse(txtanswer.Text, out userInput))
    {
            if (userInput == answer)
            {
                // Display success message
                MessageBox.Show("Correct!");
            }
            else 
            {
                // Display wrong answer message
                MessageBox.Show("Incorrect!");
            }
    }
    else
    {
       MessageBox.Show("Please enter a valid answer!");
    } 
}

private void exitButton_Click(object sender, EventArgs e)
{
    //Closes Application
    this.Close();
}

private void button4_Click(object sender, EventArgs e)
{
    //Clears text box
    txtanswer.Text = "";
}