2
if (getchar == '+') {
    answer = getnum1+getnum2;   // if the random operation is add, it will add
    addtemp++;                 // <---- disregard this
    if (answer == getanswer)   //  the answer from my textbox which is
    {                         //  user-input it is stored on "getanswer"
        correct++;            // it is compared if its correct or wrong
        addcomp++;
    }
    else { wrong++; }
}
else if (getchar == '-') {
    subtemp++;
    answer = nextValue - nextValue1;
    if (answer == getanswer) {
        correct++;
        subcomp++;
    }
    else { wrong++; }
}
else if (getchar == '*') {
    multemp++;
    answer = nextValue * nextValue1;
    if (answer == getanswer) {
        correct++;
        mulcomp++;
    }
    else { wrong++; }
}
else if (getchar == '/') {
    divtemp++;
    answer = nextValue / nextValue1;
    if (answer == getanswer) {
        correct++;
        divcomp++;
    }
    else { wrong++; }
}
else if (getchar == '%') {
    modtemp++;
    answer = nextValue % nextValue1;
    if (answer == getanswer) {
        correct++;
        modcomp++;
    }
    else { wrong++; }
}

C# 编程帮助!现在,每当我按下“分数”按钮时,它都是 MessageBox.Show(正确或错误),值是错误的。它有时会增加纠正但只有一次或两次。我的代码有问题吗?

///////////////////////////////////////// ///////// @boncodigo 的完整代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace A1_ALS_Noroña
{
    public partial class Form1 : Form
    {
        int minV=0, maxV=0,ques=0,tempques=1;

        int addtemp, subtemp, multemp, divtemp, modtemp;
        int addcomp, subcomp, mulcomp, divcomp, modcomp;
        int answer,getanswer;
        int getnum1, getnum2;

        char getchar;
        char[] select = new char[5];
        int count=0;
        int correct, wrong;

        public Form1()
        {
            InitializeComponent();
        }



        private void bttnstart_Click(object sender, EventArgs e)
        {
            bttnanswer.Enabled = true;
            grpbox1.Enabled = false;
            bttnanswer.Enabled = true;
            lblnum1.Visible = true;
            lblnum2.Visible = true;
            lbloperator.Visible = true;
            bttnstop.Enabled = true;
            bttnscore.Enabled = true;
            bttnstart.Enabled = false;


            Random random = new Random();
            int nextValue = random.Next(minV, maxV);
            int nextValue1 = random.Next(minV, maxV);
            lblnum1.Text = nextValue.ToString();
            lblnum2.Text = nextValue1.ToString();

            var rand = new Random();

            char num = select[rand.Next(count)];
            lbloperator.Text = Convert.ToString(num);
        }

        private void txtboxmin_TextChanged(object sender, EventArgs e)
        {
            minV = Convert.ToInt32(txtboxmin.Text);
        }

        private void txtbxmax_TextChanged(object sender, EventArgs e)
        {
            maxV = Convert.ToInt32(txtbxmax.Text);
        }

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

        private void bttnstop_Click(object sender, EventArgs e)
        {
            MessageBox.Show("APPLICATION STOP! The application will restart.");
            Application.Restart();
        }



        private void bttnanswer_Click(object sender, EventArgs e)
        {

            tempques++;
            Random random = new Random();
            int nextValue = random.Next(minV,maxV);
            int nextValue1 = random.Next(minV, maxV);
            lblnum1.Text = nextValue.ToString();

            var rand = new Random();
            char num = select[rand.Next(count)];
            lbloperator.Text = Convert.ToString(num);
            lblnum2.Text = nextValue1.ToString();
            getnum1 = Convert.ToInt32(lblnum1.Text);
            getnum2 = Convert.ToInt32(lblnum2.Text);
            getanswer = Convert.ToInt32(txtbxans.Text);
            getchar = Convert.ToChar(lbloperator.Text);

            if (getchar == '+')
            {

                answer = getnum1 + getnum2;
                addtemp++;              
                if (answer == getanswer)  
                {                      
                    correct++;            
                    addcomp++;
                }
                else
                {
                    wrong++;
                }
            }
            else if (getchar == '-')
            {
                subtemp++;
                answer = nextValue - nextValue1;
                if (answer == getanswer)
                {
                    correct++;
                    subcomp++;
                }
                else
                {
                    wrong++;
                }
            }
            else if (getchar == '*')
            {
                multemp++;
                answer = nextValue * nextValue1;
                if (answer == getanswer)
                {
                    correct++;
                    mulcomp++;
                }
                else
                {
                    wrong++;
                }
            }
            else if (getchar == '/')
            {
                divtemp++;
                answer = nextValue / nextValue1;
                if (answer == getanswer)
                {
                    correct++;
                    divcomp++;
                }
                else
                {
                    wrong++;
                }
            }
            else if (getchar == '%')
            {
                modtemp++;
                answer = nextValue % nextValue1;
                if (answer == getanswer)
                {
                    correct++;
                    modcomp++;
                }
                else
                {
                    wrong++;
                }
            }







        }

        private void txtbxques_TextChanged(object sender, EventArgs e)
        {
            ques = Convert.ToInt32(txtbxques.Text);

        }

        private void chkbxtimer_CheckedChanged(object sender, EventArgs e)
        {
            rdoeasy.Enabled = true;
            rdomed.Enabled = true;
            rdohard.Enabled = true;
        }

        private void chkboxAdd_CheckedChanged(object sender, EventArgs e)
        {

            if (chkboxAdd.Checked == true)
            {
                select[count] = '+';
                count++;
            }
            else if (chkboxAdd.Checked == false) 
            {
                Array.Clear(select, 0, select.Length);
                count--;
            }

        }

        private void chkboxSub_CheckedChanged(object sender, EventArgs e)
        {
            if (chkboxSub.Checked == true)
            {
                select[count] = '-';
                count++;

            }
            else if (chkboxSub.Checked == false)
            {
                Array.Clear(select, 0, select.Length);
                count--;
            }

        }

        private void chkboxMul_CheckedChanged(object sender, EventArgs e)
        {
            if (chkboxMul.Checked == true)
            {
                select[count] = '*';
                count++;

            }
            else if (chkboxMul.Checked == false)
            {
                Array.Clear(select, 0, select.Length);
                count--;
            }

        }

        private void chkboxDiv_CheckedChanged(object sender, EventArgs e)
        {
            if (chkboxDiv.Checked == true)
            {
                select[count] = '/';
                count++;

            }
            else if (chkboxDiv.Checked == false)
            {
                Array.Clear(select, 0, select.Length);
                count--;
            }
        }

        private void chkboxMod_CheckedChanged(object sender, EventArgs e)
        {
            if (chkboxMod.Checked == true)
            {
                select[count] = '%';
                count++;

            }
            else if (chkboxMod.Checked == false)
            {
                Array.Clear(select, 0, select.Length);
                count--;
            }
        }

        private void bttnscore_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Correct Answer"+correct);
        }




        }
    }
4

2 回答 2

1

提前一件事:我不知道你的错误在哪里。这里只是一些提示,我认为如果您考虑它们会很有意义,以避免将来出现类似的错误:

如果我必须查看这段代码,我的主要问题是重复代码的数量,其中非常相似的多行长模式被一遍又一遍地复制。我认为在整个代码中你没有调用任何方法,而是立即在事件处理程序中实现东西,从而重复你自己并增加错误的可能性。让我们看一下这段代码:

if (chkboxSub.Checked == true)
{
    select[count] = '-';
    count++;

}
else if (chkboxSub.Checked == false)
{
    Array.Clear(select, 0, select.Length);
    count--;
}

除了当您向选择数组添加多个运算符时出现计数错误外,此代码会重复多次。让我们将代码提取到一个方法中,并使更改的位可参数化:

void AddOrRemoveOperator(bool isChecked, char operatorChar) {
  if (isChecked) {
    select[count] = operatorChar;
    count++;
  }
  else {
    Array.Clear(select, 0, select.Length);
    count--;
  }
}

现在您可以多次调用该方法,例如:

AddOrRemoveOperator(chkboxSub.Checked, '-');

下一点是缺乏 .NET 基类库知识 (BCL)。List<T>例如,使用 a而不是数组不是更容易吗?

上述方法变为:

void AddOrRemoveOperator(bool isChecked, char operatorChar) {
  if (isChecked) {
    select.Add(operatorChar);
  }
  else {
    select.Clear();
  }
}

观察:除加法运算外的所有运算符都使用值 nextValue、nextValue1,而加法运算则使用 getnum1 和 2。这是有意的吗?

除了将代码块提取bttnanswer_Click到自己的类中之外,您还可以将重复代码提取到方法中:

 void PerformComparison(Func<int> answerProvider, 
    ref int operatorCount, 
    ref int operatorSpecificCorrectCount)
 {
    var answer = answerProvider();
    operatorCount++;
    if (answer == getanswer) {
        correct++;
        operatorSpecificCorrectCount++;
    }
    else {
        wrong++;
    }
 }

该代码仍然会让我发疯(因为您编写的课程缺乏凝聚力),但我们已经与代码重复作斗争。现在您可以像这样调用该方法:

if (getchar == '+')
{
  PerformComparison(()=>getnum1 + getnum2, ref addtemp, ref addcomp);
}

有许多技术可以将代码变形为更易于测试和维护(重构)的形式,到目前为止我们只使用了提取方法。有关更多技术的书重构:改进现有代码的设计仍然是强烈推荐的。

于 2012-11-25T15:04:31.137 回答
0

这可能是浮点精度问题(可能您的用户输入 3 并且程序计算 2.9999999)。调试您的代码并确定未正确添加的一种情况。

于 2012-11-25T13:15:54.083 回答