[英]How to display results from selected radio buttons into one textbox using a for loop?
[英]How to calculate and display average grade from radio button values in a textbox using a for loop?
我正在制作一個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
這里的問題是您必須檢查(value <= x && value >= y)
但是要檢查的是它是否小於或等於<=
這是一個片段:
if (avgCourseScore >= 4 && avgCourseScore < 5)
{
txtCourseGrade.Text = "B";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.