C# 如何使用for循环将所选单选按钮的结果显示到一个文本框中?

C# 如何使用for循环将所选单选按钮的结果显示到一个文本框中?,c#,html,asp.net,radio-button,server-side,C#,Html,Asp.net,Radio Button,Server Side,我正在制作一个html表单,当用户提交表单时,将加载一个aspx页面来显示表单的结果。我一共有20个问题,每个问题有5个单选按钮。由于某些原因,当我试图通过For循环检索所选单选按钮时,它只显示一个答案。我做错了什么 我曾尝试更改for循环中的值,但它似乎没有正确显示 这是aspx.cs的代码 public void DisplayResults(double[] results) { try { int q = 1;

我正在制作一个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");
        }
    }
public void显示结果(双[]结果)
{
尝试
{  
int q=1;

对于(inti=0;i首先:这是因为你没有增加q值

不正确,因为q始终为1:

其次,您允许程序为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");
    }
}
public void显示结果(双[]结果)
{
尝试
{  

对于(inti=0,q=1;iFirst):这是因为你没有增加q值

不正确,因为q始终为1:

其次,您允许程序为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");
    }
}
public void显示结果(双[]结果)
{
尝试
{  

对于(int i=0,q=1;i它仍然给我相同的结果。您还犯了一个错误,我现在将更新我的答案。如果
i
为零,它将尝试查找不存在的Q0?@jasonaler OP只是将结果显示为
Q0:Agree,Q1:Agree
,而不是操纵它……顺便说一句,谢谢您让我知道,我将更新它。)答案一个改进可能是使用一个对象数组来保存(问题类型、值、文本响应)然后,重复的if语句可以根据问题类型和值进行查找,以获得文本响应。它仍然会给我相同的结果您还犯了一个错误,我现在将更新我的答案如果
I
为零,它将尝试查找不存在的Q0?@Jasonaler OP只是将结果显示为
Q0:同意,Q1:不同意,不要操纵它…顺便说一句,谢谢你让我知道,我会更新答案。改进可能是使用一个对象数组来保存(问题类型、值、文本响应),然后重复的if语句可以通过基于问题类型和值的查找来获得文本响应。
txtResults.Text = "Course Results";
if (results[i] == 1)
{
    txtResults.Text += "Q" + q + ": Strongly Disagree";
}
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");
    }
}