如何计算 "correct" 和 "incorrect" 响应的数量

How to count the number of "correct" and "incorrect" responses

我是编码新手,正在用 C# 创建一个 windows 表单应用程序。我很难弄清楚如何跟踪 correctincorrect 响应的数量。单击按钮时,如果响应为 correct,则标签表示正确。我想要一个不同的标签来计算它说正确和不正确的次数。这是我的想法,但没有奏效。

int add = 0;
add = int.Parse(correct.Text);

if (label1.Text == "Correct")
{
    correct.Text = add++;
}
else
    incorrect.text = add++;

correctincorrect 是我的标签名称。

这是我的按钮点击事件,有很多相似的。

private void G_Click(object sender, EventArgs e)
        {
            if (go.Visible == true)
            {
                go.Visible = false;
                Random rnd = new Random();
                int y = rnd.Next(1, 7);

                if (y == 1)
                {
                    eo.Visible = true;
                }
                if (y == 2)
                {
                    ao.Visible = true;
                }
                if (y == 4)
                {
                    dd.Visible = true;
                }
                if (y == 5)
                {
                    go.Visible = true;
                }
                if (y == 6)
                {
                    eeo.Visible = true;
                }

                timer1.Start();

                label1.Text = "Correct";
            }
            else
             {
                label1.Text = "Incorrect";
             }

            private int correctCount = 0;
            private int incorrectCount = 0;


             if (label1.Text = "Correct";)
                {
                     correctCount++;
                     correct.Text = correctCount.ToString();
                }
    else
                {
                      incorrectCount++;
                      incorrect.Text = incorrectCount.ToString();
                }

眼前的问题是 Text 属性 的类型是 string,而不是 int - 您必须将其设置为 text ,不是数字。幸运的是,您只需在 int 上调用 ToString 即可获得 string 表示。但是,我建议还有其他需要更改的地方。

我强烈建议您将单独的计数器作为变量。虽然您 可以 继续解析您的标签,但将它们纯粹输出更简单。所以你可能有:

// Fields
private int correctCount = 0;
private int incorrectCount = 0;
...
// Wherever your code is
if (correctAnswer)
{
    correctCount++;
    correct.Text = correctCount.ToString();
}
else
{
    incorrectCount++;
    incorrect.Text = incorrectCount.ToString();
}

请注意此处的 correctAnswer 条件 - 我们不知道 label1 的文本是什么设置的,但我建议 也是 改变 correct/incorrect 计数器,从相同的条件。同样,将 label1 视为输出,而不是反馈系统。

class 字段 = 在方法外直接在 class
内声明的变量 局部变量 = 在方法内部声明的变量

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

        private int correctCount = 0; // field

        private int incorrectCount = 0; // field

        private void G_Click(object sender, EventArgs e)
        {
            if (go.Visible == true)
            {
                go.Visible = false;
                Random rnd = new Random();
                int y = rnd.Next(1, 7); // local variable

                if (y == 1)
                {
                    eo.Visible = true;
                }
                if (y == 2)
                {
                    ao.Visible = true;
                }
                if (y == 4)
                {
                    dd.Visible = true;
                }
                if (y == 5)
                {
                    go.Visible = true;
                }
                if (y == 6)
                {
                    eeo.Visible = true;
                }

                timer1.Start();

                incorrect.Text = "Correct";
            }
            else
            {
                incorrect.Text = "Incorrect";
            }


            if (label1.Text = "Correct")
            {
                correctCount++;
                correct.Text = correctCount.ToString();
            }
            else
            {
                incorrectCount++;
                incorrect.Text = incorrectCount.ToString();
            }
        }
    }
}

== 平等。 (用于比较 2 个值)
= 赋值。 (用于初始化变量或字段)