【问题标题】:How to calculate and display average grade from radio button values in a textbox using a for loop?如何使用 for 循环从文本框中的单选按钮值计算和显示平均成绩?
【发布时间】:2019-06-30 03:37:23
【问题描述】:

我正在制作一个 html 表单,当用户提交表单时,将加载一个 aspx 页面以显示表单的结果。我总共有 20 个问题,每个问题有 5 个单选按钮。每个单选按钮都有一个从 1 到 5 的值(强烈反对为 1,强烈同意为 2)。我想在提交表单后计算平均分数并将其显示到文本框中。

我尝试使用 for 循环遍历分数并将它们加在一起,然后得到平均值。然后我使用了 if/else 语句并尝试相应地更改文本框等级。

当我尝试显示成绩时,无论我做出什么选择,结果总是显示为 A。

这是我的 aspx.cs 代码

protected void Page_Load(object sender, EventArgs e)
    {
        txtStudentName.Text = Request["txtFirstName"].ToString() + " " + Request["txtLastName"].ToString();
        txtID.Text = Request["txtID"].ToString();
        txtCourse.Text = Request["dbCourses"].ToString();

        double[] scores =
        {
            double.Parse(Request["q1"]),
            double.Parse(Request["q2"]),
            double.Parse(Request["q3"]),
            double.Parse(Request["q4"]),
            double.Parse(Request["q5"]),
            double.Parse(Request["q6"]),
            double.Parse(Request["q7"]),
            double.Parse(Request["q8"]),
            double.Parse(Request["q9"]),
            double.Parse(Request["q10"]),
            double.Parse(Request["q11"]),
            double.Parse(Request["q12"]),
            double.Parse(Request["q13"]),
            double.Parse(Request["q14"]),
            double.Parse(Request["q15"]),
            double.Parse(Request["q16"]),
            double.Parse(Request["q17"]),
            double.Parse(Request["q18"]),
            double.Parse(Request["q19"]),
            double.Parse(Request["q20"])
        };

        DisplayResults(scores);
        CalculateGrades(scores);
    }

    public void CalculateGrades(double[] scores)
    {
        double courseScore = 0;
        double profScore = 0;

        for (int i = 0; i < 12; i++)
        {
            courseScore += scores[i];
        }

        double avgCourseScore = courseScore / 12.0;

        if (avgCourseScore <= 5)
        {
            txtCourseGrade.Text = "A";
        }
        else if (avgCourseScore <= 4)
        {
            txtCourseGrade.Text = "B";
        }
        else if (avgCourseScore <= 3)
        {
            txtCourseGrade.Text = "C";
        }
        else if (avgCourseScore <= 2)
        {
            txtCourseGrade.Text = "D";
        }
        else if (avgCourseScore <= 1)
        {
            txtCourseGrade.Text = "F";
        }



        for (int j = 12; j < 20; j++)
        {
            profScore += scores[j];
        }

        double avgProfScore = profScore / 8.0;

        if (avgProfScore <= 5)
        {
            txtProfGrade.Text = "A";
        }
        else if (avgProfScore <= 4)
        {
            txtProfGrade.Text = "B";
        }
        else if (avgProfScore <= 3)
        {
            txtProfGrade.Text = "C";
        }
        else if (avgProfScore <= 2)
        {
            txtProfGrade.Text = "D";
        }
        else if (avgProfScore <= 1)
        {
            txtProfGrade.Text = "F";
        }
    }

这是我的 aspx 的代码

        <fieldset>
            <legend>Final Grade</legend>
            <asp:Label ID="lblCourseGrade" runat="server">Course Grade: </asp:Label>
            <asp:TextBox ID="txtCourseGrade" runat="server" BorderStyle="None" ReadOnly="True"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblProfGrade" runat="server">Professor Grade: </asp:Label>
            <asp:TextBox ID="txtProfGrade" runat="server" BorderStyle="None" ReadOnly="True"></asp:TextBox>
        </fieldset>

这是我的html代码

  <h3>Please answer these questions to the best of your knowledge:</h3>
    <fieldset>
        <legend>Course Content (Organization, Clarity of Expectations/Directions, Balance/Appropriateness)</legend>
        1. The course (or section) presented skills in a helpful sequence
        <br />
        <input type="radio" name="q1" value="5" />strongly agree
        <input type="radio" name="q1" value="4" />agree
        <input type="radio" name="q1" value="3" />neutral
        <input type="radio" name="q1" value="2" />disagree
        <input type="radio" name="q1" value="1" />strongly disagree
        <br />
        <br />
        2. The course (or section) provided an appropriate balance between instruction and practice
        <br />
        <input type="radio" name="q2" value="5" />strongly agree
        <input type="radio" name="q2" value="4" />agree
        <input type="radio" name="q2" value="3" />neutral
        <input type="radio" name="q2" value="2" />disagree
        <input type="radio" name="q2" value="1" />strongly disagree
        <br />
        <br />
        3. The course (or section) was appropriate for the stated level of the class
        <br />
        <input type="radio" name="q3" value="5" />strongly agree
        <input type="radio" name="q3" value="4" />agree
        <input type="radio" name="q3" value="3" />neutral
        <input type="radio" name="q3" value="2" />disagree
        <input type="radio" name="q3" value="1" />strongly disagree
        <br />
        <br />
        4. The course (or section) was organized in a way that helped me learn
        <br />
        <input type="radio" name="q4" value="5" />strongly agree
        <input type="radio" name="q4" value="4" />agree
        <input type="radio" name="q4" value="3" />neutral
        <input type="radio" name="q4" value="2" />disagree
        <input type="radio" name="q4" value="1" />strongly disagree
        <br />
        <br />
        5. The lab helped to complement the lectures
        <br />
        <input type="radio" name="q5" value="5" />strongly agree
        <input type="radio" name="q5" value="4" />agree
        <input type="radio" name="q5" value="3" />neutral
        <input type="radio" name="q5" value="2" />disagree
        <input type="radio" name="q5" value="1" />strongly disagree
        <br />
        <br />
        6. The course (or section) provided a mixture of explanation and practice
        <br />
        <input type="radio" name="q6" value="5" />strongly agree
        <input type="radio" name="q6" value="4" />agree
        <input type="radio" name="q6" value="3" />neutral
        <input type="radio" name="q6" value="2" />disagree
        <input type="radio" name="q6" value="1" />strongly disagree
        <br />
        <br />
        7. The course (or section) was effectively organized
        <br />
        <input type="radio" name="q7" value="5" />strongly agree
        <input type="radio" name="q7" value="4" />agree
        <input type="radio" name="q7" value="3" />neutral
        <input type="radio" name="q7" value="2" />disagree
        <input type="radio" name="q7" value="1" />strongly disagree
        <br />
        <br />
        8. The course (or section) assignments and lectures usefully complemented each other
        <br />
        <input type="radio" name="q8" value="5" />strongly agree
        <input type="radio" name="q8" value="4" />agree
        <input type="radio" name="q8" value="3" />neutral
        <input type="radio" name="q8" value="2" />disagree
        <input type="radio" name="q8" value="1" />strongly disagree
        <br />
        <br />
        9. The course (or section) instructions (including, manuals, handouts, etc.) were clear
        <br />
        <input type="radio" name="q9" value="5" />strongly agree
        <input type="radio" name="q9" value="4" />agree
        <input type="radio" name="q9" value="3" />neutral
        <input type="radio" name="q9" value="2" />disagree
        <input type="radio" name="q9" value="1" />strongly disagree
        <br />
        <br />
        10. The course (or section) work helped me understand concepts more clearly
        <br />
        <input type="radio" name="q10" value="5" />strongly agree
        <input type="radio" name="q10" value="4" />agree
        <input type="radio" name="q10" value="3" />neutral
        <input type="radio" name="q10" value="2" />disagree
        <input type="radio" name="q10" value="1" />strongly disagree
        <br />
        <br />
        11. Instructions for course (or section) materials (including manuals, handouts, etc.) were clear
        <br />
        <input type="radio" name="q11" value="5" />strongly agree
        <input type="radio" name="q11" value="4" />agree
        <input type="radio" name="q11" value="3" />neutral
        <input type="radio" name="q11" value="2" />disagree
        <input type="radio" name="q11" value="1" />strongly disagree
        <br />
        <br />
        12. The lab complemented my understanding of the lectures
        <br />
        <input type="radio" name="q12" value="5" />strongly agree
        <input type="radio" name="q12" value="4" />agree
        <input type="radio" name="q12" value="3" />neutral
        <input type="radio" name="q12" value="2" />disagree
        <input type="radio" name="q12" value="1" />strongly disagree
    </fieldset>
    <br />
    <br />

    <fieldset>
        <legend> Instructor Specific Questions</legend>
        1. The instructor clearly presented the skills to be learned
        <br />
        <input type="radio" name="q13" value="5" />strongly agree
        <input type="radio" name="q13" value="4" />agree
        <input type="radio" name="q13" value="3" />neutral
        <input type="radio" name="q13" value="2" />disagree
        <input type="radio" name="q13" value="1" />strongly disagree
        <br />
        <br />
        2. The instructor effectively presented concepts and techniques
        <br />
        <input type="radio" name="q14" value="5" />strongly agree
        <input type="radio" name="q14" value="4" />agree
        <input type="radio" name="q14" value="3" />neutral
        <input type="radio" name="q14" value="2" />disagree
        <input type="radio" name="q14" value="1" />strongly disagree
        <br />
        <br />
        3. The instructor presented content in an organized manner
        <br />
        <input type="radio" name="q15" value="5" />strongly agree
        <input type="radio" name="q15" value="4" />agree
        <input type="radio" name="q15" value="3" />neutral
        <input type="radio" name="q15" value="2" />disagree
        <input type="radio" name="q15" value="1" />strongly disagree
        <br />
        <br />
        4. The instructor effectively presented the tools (e.g. materials, skills, and techniques) needed
        <br />
        <input type="radio" name="q16" value="5" />strongly agree
        <input type="radio" name="q16" value="4" />agree
        <input type="radio" name="q16" value="3" />neutral
        <input type="radio" name="q16" value="2" />disagree
        <input type="radio" name="q16" value="1" />strongly disagree
        <br />
        <br />
        5. The instructor explained concepts clearly
        <br />
        <input type="radio" name="q17" value="5" />strongly agree
        <input type="radio" name="q17" value="4" />agree
        <input type="radio" name="q17" value="3" />neutral
        <input type="radio" name="q17" value="2" />disagree
        <input type="radio" name="q17" value="1" />strongly disagree
        <br />
        <br />
        6. The instructor made the elements of good writing clear
        <br />
        <input type="radio" name="q18" value="5" />strongly agree
        <input type="radio" name="q18" value="4" />agree
        <input type="radio" name="q18" value="3" />neutral
        <input type="radio" name="q18" value="2" />disagree
        <input type="radio" name="q18" value="1" />strongly disagree
        <br />
        <br />
        7. The instructor clearly articulated the standards of performance for the course
        <br />
        <input type="radio" name="q19" value="5" />strongly agree
        <input type="radio" name="q19" value="4" />agree
        <input type="radio" name="q19" value="3" />neutral
        <input type="radio" name="q19" value="2" />disagree
        <input type="radio" name="q19" value="1" />strongly disagree
        <br />
        <br />
        8. The instructor provided guidance for understanding course exercises
        <br />
        <input type="radio" name="q20" value="5" />strongly agree
        <input type="radio" name="q20" value="4" />agree
        <input type="radio" name="q20" value="3" />neutral
        <input type="radio" name="q20" value="2" />disagree
        <input type="radio" name="q20" value="1" />strongly disagree
    </fieldset>

我希望能够根据相应文本框中单选按钮提交的值显示平均字母等级:

示例....

课程成绩:A

教授级:F

【问题讨论】:

  • 您的问题/疑问是什么?我们是否应该猜测您想问我们什么?
  • 对不起,我忘了提这个问题。我已经在上面更新了。
  • 啊,我明白了。查看用于设置txtCourseGrade.Textif/else 语句的逻辑/条件。

标签: c# html asp.net radio-button


【解决方案1】:

这里的问题是您必须检查(value &lt;= x &amp;&amp; value &gt;= y),但您要检查的是它是否小于或等于&lt;=

这是一个sn-p:

if (avgCourseScore >= 4 && avgCourseScore < 5)
{
    txtCourseGrade.Text = "B";
}

【讨论】:

  • 仅供参考:虽然您的回答肯定是解决问题的一种方式,但请注意,问题不在于使用 &lt;= 本身。 if 语句的表达式可以保持简单,只需将整个 if/else 语句链颠倒过来...
  • 是的,@elgonzo 你是对的。反转if/else 语句也将正常工作
猜你喜欢
  • 1970-01-01
  • 2016-12-22
  • 2019-08-25
  • 2014-11-03
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多