变量不会增加/减少我的代码

时间:2012-11-25 13:11:30

标签: c# messagebox

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#编程帮助!现在每当按下“SCORES”按钮时,它就是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);
        }




        }
    }

2 个答案:

答案 0 :(得分:1)

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

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

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

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

除了将几个运算符添加到select-array中的计数错误之外,此代码会重复多次。让我们将代码提取到一个方法中,并使可变参数化的位变为:

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>代替数组会不会更容易?

上述方法变为:

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);
}

有许多技术可以将代码转换为更容易测试和维护(重构)的表单,到目前为止我们只使用了提取方法。对于更多技巧,本书Refactoring: Improving the Design of Existing Code仍然值得推荐。

答案 1 :(得分:0)

它可能是浮点精度问题(可能您的用户输入3并且程序计算2.9999999)。调试代码并在未正确添加时识别一个案例。