无法弄清楚为什么不能正确地进行等级计算

时间:2014-03-31 22:26:20

标签: c# asp.net web-controls

我的问题是,当我尝试用低于25/25的作业计算成绩时,成绩达到75%。无视我的部分中的任何其他要点。

因此,如果每个作业都有100%的作品,我会得到:

Grade   A
Percentage  105%
Labs    250 / 250

但是当实验室失去1分时:

Grade   C
Percentage  75%
Labs    249 / 250

为什么会这样?我对此感到困惑。

这是我从文本字段收集输入的方式。每个文本字段都在调用:AutoPostBack="True" OnTextChanged="textbox_TextChanged"

    foreach (Control control in Page.Controls)
    {
        foreach (Control textbox in control.Controls)
        {
            if (textbox is TextBox)
            {
                TextBox tb = textbox as TextBox;

                char type = tb.ID.ToCharArray()[0];

                if (!string.IsNullOrWhiteSpace(tb.Text))
                {
                    if (type == 'l')
                    {
                        int outtt;
                        string inn = tb.Text;
                        Int32.TryParse(inn, out outtt);

                        labs += outtt;
                        labPoints += 25;
                    }
                    else if (type == 'q')
                    {
                        ...
                    }
                    ...
                }
            }
        }
    }

这是我计算百分比的方式:

        percent = (((labs / labPoints) * 0.3) + // labs
                ((quizzes / quizPoints) * 0.1) + // quizzes
                ((exams / examPoints) * 0.3) + // exams
                ((project / projectPoints) * 0.2) + // project
                ((extra / extraPoints) * 0.05) + // extra credit
                ((part / partPoints) * 0.10)) * 100;// participation

1 个答案:

答案 0 :(得分:4)

您正在使用整数除法,它将始终生成整数。对于快速和脏的修复,您应该在划分它们时将变量转换为某些浮点变量。

e.g:

(double)labs / (double)labPoints

(如果任何参数为double,则结果为double。)

相关问题