![](/img/trans.png)
[英]How to calculate and display average grade from radio button values in a textbox using a for loop?
[英]How to display results from selected radio buttons into one textbox using a for loop?
我正在制作一個html表單,當用戶提交表單時,將加載一個aspx
頁面以顯示表單的結果。 我總共有20個問題,每個問題有5個單選按鈕。 由於某些原因,當我嘗試通過for循環檢索選定的單選按鈕時,它僅顯示一個答案。 我究竟做錯了什么?
我嘗試過更改for循環中的值,但似乎無法正確顯示。
這是aspx.cs的代碼
public void DisplayResults(double[] results)
{
try
{
int q = 1;
for (int i = 0; i <= 19; i++)
{
if (i == 0)
{
txtResults.Text = "Course Results";
if (results[i] == 1)
{
txtResults.Text += "Q" + q + ": Strongly Disagree";
}
else if (results[i] == 2)
{
txtResults.Text += "Q" + q + ": Disagree";
}
else if (results[i] == 3)
{
txtResults.Text += "Q" + q + ": Neutral";
}
else if (results[i] == 4)
{
txtResults.Text += "Q" + q + ": Agree";
}
else if (results[i] == 5)
{
txtResults.Text += "Q" + q + ": Strongly Agree ZERO";
}
}
else if (i == 11)
{
txtResults.Text = "Professor Results";
if (results[i] == 1)
{
txtResults.Text += "Q" + q + ": Strongly Disagree";
}
else if (results[i] == 2)
{
txtResults.Text += "Q" + q + ": Disagree";
}
else if (results[i] == 3)
{
txtResults.Text += "Q" + q + ": Neutral";
}
else if (results[i] == 4)
{
txtResults.Text += "Q" + q + ": Agree";
}
else if (results[i] == 5)
{
txtResults.Text += "Q" + q + ": Strongly Agree ELEVEN";
}
}
else
{
if (results[i] == 1)
{
txtResults.Text += "Q" + q + ": Strongly Disagree";
}
else if (results[i] == 2)
{
txtResults.Text += "Q" + q + ": Disagree";
}
else if (results[i] == 3)
{
txtResults.Text += "Q" + q + ": Neutral";
}
else if (results[i] == 4)
{
txtResults.Text += "Q" + q + ": Agree";
}
else if (results[i] == 5)
{
txtResults.Text += "Q" + q + ": Strongly Agree ELSE";
}
}
if (txtResults.Text == "")
{
throw new Exception();
}
}
}
catch (Exception e)
{
Console.Write(e.Message, "Error");
}
}
這是aspx的代碼
<fieldset>
<legend>Feedback Results</legend>
<asp:TextBox ID="txtResults" runat="server" TextMode="MultiLine" Width="100%" Height="500px"></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>
我希望能夠像這樣在同一行多行文本框中顯示每個問題的每個結果,但將其分為課程和教授問題:
課程
問題1:不同意
Q2:中立
Q3:中立
等等...
教授
問題1:不同意
Q2:中性
Q3:中立
等等...
第一:這是因為您沒有增加q值,
不正確,因為q始終為1:
txtResults.Text = "Course Results";
if (results[i] == 1)
{
txtResults.Text += "Q" + q + ": Strongly Disagree";
}
其次,您已允許該程序針對i = 0和i = 11的情況進行編寫
完整答案:
public void DisplayResults(double[] results)
{
try
{
for (int i = 0, q = 1; i <= 19; i++, q++)
{
if (i == 0)
{
txtResults.Text += "Course Results";
}
if (i == 11)
{
txtResults.Text += "Professor Results";
}
txtResults.Text += "Q" + q ": ";
if (results[i] == 1)
{
txtResults.Text +="Strongly Disagree";
}
else if (results[i] == 2)
{
txtResults.Text +="Disagree";
}
else if (results[i] == 3)
{
txtResults.Text +=": Neutral";
}
else if (results[i] == 4)
{
txtResults.Text +="Agree";
}
else if (results[i] == 5)
{
if(i == 0 )
txtResults.Text +="Strongly Agree ZERO";
else if(i == 11 )
txtResults.Text +="Strongly Agree ELEVEN";
else
txtResults.Text +="Strongly Agree";
}
}
}
catch (Exception e)
{
Console.Write(e.Message, "Error");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.