简体   繁体   English

C# windows 表格项目除法变量错误

[英]C# windows form project Divide variable error

Greeting everyone, I'm trying to do a windows form for collage were I can get the average of student scores.问候大家,我正在尝试做一个 windows 的拼贴表格,我可以得到学生平均分数。 Seems like I have an issue with the divide function. here is my code, to see if you guys spot something wrong that could help me.好像我对 function 有问题。这是我的代码,看看你们是否发现了可以帮助我的错误。 I Appreciate in advance我提前感谢

    private void getaveragebutton_Click(object sender, EventArgs e)
    {
        if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
            customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
            || customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
            customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
        {
            MessageBox.Show("Fill all the Boxes with Digits");
        }

        else
        {

            label4.Visible = true;
            label5.Visible = true;
            label7.Visible = true;
            label61.Visible = true;
            label28.Visible = true;
            label29.Visible = true;
            label30.Visible = true;
            label31.Visible = true;
            label45.Visible = true;
            label46.Visible = true;


            //short--test calculations

            //students short test score variables
            decimal a, b, c, d, e1, f, g, h, i, j;
            //students short test value variables
            decimal k, l, m, n, o, p, q, r, s, t;
            //st results variables
            decimal u, v;
            decimal r1st;
            decimal r2st;
            decimal results;

            //customboxes st score
            a = Decimal.Parse(customTextboxes4.Texts);
            b = Decimal.Parse(customTextboxes5.Texts);
            c = Decimal.Parse(customTextboxes6.Texts);
            d = Decimal.Parse(customTextboxes7.Texts);
            e1 = Decimal.Parse(customTextboxes8.Texts);
            f = Decimal.Parse(customTextboxes9.Texts);
            g = Decimal.Parse(customTextboxes10.Texts);
            h = Decimal.Parse(customTextboxes11.Texts);
            i = Decimal.Parse(customTextboxes12.Texts);
            j = Decimal.Parse(customTextboxes13.Texts);

            //custom boxes st values
            k = Decimal.Parse(customTextboxes15.Texts);
            l = Decimal.Parse(customTextboxes16.Texts);
            m = Decimal.Parse(customTextboxes17.Texts);
            n = Decimal.Parse(customTextboxes18.Texts);
            o = Decimal.Parse(customTextboxes19.Texts);
            p = Decimal.Parse(customTextboxes20.Texts);
            q = Decimal.Parse(customTextboxes21.Texts);
            r = Decimal.Parse(customTextboxes22.Texts);
            s = Decimal.Parse(customTextboxes23.Texts);
            t = Decimal.Parse(customTextboxes24.Texts);

            r1st = a + b + c + d + e1 + f + g + h + i + j;
            r2st = k + l + m + n + o + p + q + r + s + t;
            //st results variables definitions
            u = Decimal.Parse(label28.Text);
            v = Decimal.Parse(label29.Text);
           
           
            //st result



            label28.Text = r1st.ToString();
            label29.Text = r2st.ToString();
            results = u / v;
            label4.Text = results.ToString();
           
            return;
        }

[ enter image description here ]( https://i.stack.imgur.com/xu8cp.png ) [在此处输入图片描述]( https://i.stack.imgur.com/xu8cp.png )

Im trying to get all the score inserted in the text boxes to sum them.我试图将所有分数插入文本框中以对它们求和。 then get the results to Student Score and Total (That part is working for me).然后将结果获取到 Student Score 和 Total(这部分对我有用)。 After that divide the Student Score / Total to get the results in a hidden label that will be visible after getting the results.之后将学生分数/总分相除,得到隐藏的 label 中的结果,在获得结果后将可见。 (that is the part that Im having issues with) (这是我遇到问题的部分)

Well sorry to everyone for not following the proper way to post to get the proper help and thanks for those who let me know what to do for a next time to get the proper help.很抱歉没有按照正确的方式发帖以获得适当的帮助,并感谢那些让我知道下次该怎么做以获得适当帮助的人。 Also, thank you for explaining me the variable thing that was empty I really appreciate.另外,感谢您向我解释空的变量,我真的很感激。 Here is how I did fix my issue:这是我解决问题的方法:

    private void getaveragebutton_Click(object sender, EventArgs e)
    {
        if (customTextboxes4.Texts == "" || customTextboxes5.Texts == "" || customTextboxes6.Texts == "" || customTextboxes7.Texts == "" || customTextboxes8.Texts == "" ||
            customTextboxes9.Texts == "" || customTextboxes10.Texts == "" || customTextboxes11.Texts == "" || customTextboxes12.Texts == "" || customTextboxes13.Texts == ""
            || customTextboxes15.Texts == "" || customTextboxes16.Texts == "" || customTextboxes17.Texts == "" || customTextboxes18.Texts == "" || customTextboxes19.Texts == "" ||
            customTextboxes20.Texts == "" || customTextboxes21.Texts == "" || customTextboxes22.Texts == "" || customTextboxes23.Texts == "" || customTextboxes24.Texts == "")
        {
            MessageBox.Show("Fill all the Boxes with Digits");
        }

        else
        {

            label4.Visible = true;
            label5.Visible = true;
            label7.Visible = true;
            label61.Visible = true;
            label28.Visible = true;
            label29.Visible = true;
            label30.Visible = true;
            label31.Visible = true;
            label45.Visible = true;
            label46.Visible = true;


            //short--test calculations

            //students short test score variables
            decimal studentscorebox1, studentscorebox2, studentscorebox3, studentscorebox4, studentscorebox5, studentscorebox6, studentscorebox7, studentscorebox8, studentscorebox9, studentscorebox10;
            //students short test value variables
            decimal studentshorttestvalue1, studentshorttestvalue2, studentshorttestvalue3, studentshorttestvalue4, studentshorttestvalue5, studentshorttestvalue6, studentshorttestvalue7, studentshorttestvalue8,
                studentshorttestvalue9, studentshorttestvalue10;
            //st results variables
            
            decimal studentshortestoverallscore;
            decimal shortestoverallvalue;
            decimal shortestaverage;

            //customboxes student short test scores
            studentscorebox1 = Decimal.Parse(customTextboxes4.Texts);
            studentscorebox2 = Decimal.Parse(customTextboxes5.Texts);
            studentscorebox3 = Decimal.Parse(customTextboxes6.Texts);
            studentscorebox4 = Decimal.Parse(customTextboxes7.Texts);
            studentscorebox5 = Decimal.Parse(customTextboxes8.Texts);
            studentscorebox6 = Decimal.Parse(customTextboxes9.Texts);
            studentscorebox7 = Decimal.Parse(customTextboxes10.Texts);
            studentscorebox8 = Decimal.Parse(customTextboxes11.Texts);
            studentscorebox9 = Decimal.Parse(customTextboxes12.Texts);
            studentscorebox10 = Decimal.Parse(customTextboxes13.Texts);

            //student short test value
            studentshorttestvalue1 = Decimal.Parse(customTextboxes15.Texts);
            studentshorttestvalue2 = Decimal.Parse(customTextboxes16.Texts);
            studentshorttestvalue3 = Decimal.Parse(customTextboxes17.Texts);
            studentshorttestvalue4 = Decimal.Parse(customTextboxes18.Texts);
            studentshorttestvalue5 = Decimal.Parse(customTextboxes19.Texts);
            studentshorttestvalue6 = Decimal.Parse(customTextboxes20.Texts);
            studentshorttestvalue7 = Decimal.Parse(customTextboxes21.Texts);
            studentshorttestvalue8 = Decimal.Parse(customTextboxes22.Texts);
            studentshorttestvalue9 = Decimal.Parse(customTextboxes23.Texts);
            studentshorttestvalue10 = Decimal.Parse(customTextboxes24.Texts);

            //Sumatory of Students scores and Sumatory of Test Value
            studentshortestoverallscore = studentscorebox1 + studentscorebox2 + studentscorebox3 + studentscorebox4 + studentscorebox5 + studentscorebox6 + studentscorebox7 + studentscorebox8 + studentscorebox9 + studentscorebox10;
            shortestoverallvalue = studentshorttestvalue1 + studentshorttestvalue2 + studentshorttestvalue3 + studentshorttestvalue4 + studentshorttestvalue5 + studentshorttestvalue6 + studentshorttestvalue7 +
                studentshorttestvalue8 + studentshorttestvalue9 + studentshorttestvalue10;
          

            //String Results to Labels

            label28.Text = studentshortestoverallscore.ToString();
            label29.Text = shortestoverallvalue.ToString();
            //Get Average of Short Test
              shortestaverage = studentshortestoverallscore / shortestoverallvalue * 100;
              label4.Text = shortestaverage.ToString();

            return;
        }

    }

I did a correction of the variable names to not get confused like I was advised in the first post, then I deleted some variables that were empty like someone here explained me (thanks).我对变量名称进行了更正,以免像第一篇文章中建议的那样混淆,然后我删除了一些空变量,就像这里有人向我解释的那样(谢谢)。 Finally I did a re order of how it should be calculated to get the average and got it working.最后,我对如何计算以获得平均值并使其正常工作进行了重新排序。 thanks to all and if you see something that I can improve let me know.感谢所有人,如果您看到我可以改进的地方,请告诉我。 I really appreciate everyone and every single advise.我真的很感谢每个人和每一个建议。 Have a good day everyone.祝大家有个美好的一天。

You cannot convert label to decimal as long as label contains no value.只要 label 不包含值,就不能将 label 转换为十进制。 You have to rearrange the lines as follows您必须按如下方式重新排列这些行

        //st result

        label28.Text = r1st.ToString();
        label29.Text = r2st.ToString();

        u = Decimal.Parse(label28.Text);
        v = Decimal.Parse(label29.Text);

        results = u / v;
        label4.Text = results.ToString();
       
        return;
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM