简体   繁体   中英

How to calculate and display average grade from radio button values in a textbox using a for loop?

I am making a html form and when the user submits the form an aspx page will load to display the results of the form. I have 20 questions total with 5 radio buttons each. Each radio button has a value from 1 to 5 (strongly disagree being 1 and strong agree being 2). I want to calculate the average score once the form is submitted and display it into a textbox.

I have tried using a for loop to iterate through the scores and add them together then getting the average. Then i used an if/else statement and try to change the textbox grade accordingly.

When I try to display the grades, not matter what choice i make the result always comes out as A for both.

Here's my aspx.cs code

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

Here's the code for my 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>

Here's my html code

  <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>

I want to be able to display an average letter grade based on the values submitted by the radio buttons in their according textboxes:

Example....

Course Grade: A

Professor Grade: F

The problem here is that you've to check for (value <= x && value >= y) but what you're checking for, is whether it is less than or equal to <=

Here's a snippet:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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