ASP.NET C#人格测试

时间:2014-11-01 23:49:50

标签: c# asp.net

我认为我使用C#在ASP.NET中进行这种性格测试的代码很好。但有时它是有效的,有时它不会。我没有得到任何语法错误,就像我说的,有时它完美无缺。我错过了一个逻辑错误吗?

<%@ Page Language="C#" Debug="true" %>

<!DOCTYPE html>
<script runat="server"> 

public int m_score = 0;
public int m_imageScore = 0;


//Displaying the autopostback message for the selection of the workList drop-down list
protected void workListChanged(object sender, EventArgs e)  
{
    if (workList.SelectedItem.Text == "Office Work")
        lblWork.Text = "You prefer to stay inside and code your life away.";
    else if (workList.SelectedItem.Text == "Outdoor Work")
        lblWork.Text = "You enjoy the great outdoors.";
    else if (workList.SelectedItem.Text == "Investigative Work")
        lblWork.Text = "OK, Sherlock.";
    else if (workList.SelectedItem.Text == "Working With People")
        lblWork.Text = "As opposed to extraterrestirals?";
    else if (workList.SelectedItem.Text == "Work Requiring Travel")
        lblWork.Text = "I can show you the world . . . ";
    else if (workList.SelectedItem.Text == "Helping People")
        lblWork.Text = "You're a noble spirit.";
}  


//Finding the score for the which part of the image the user clicked
 public void ImageButton_Click(object sender, ImageClickEventArgs e)
{

    if ((e.X > 2 && e.X < 87) && (e.Y > 2 && e.Y < 120))
        m_imageScore = 50;
    else if ((e.X > 96 && e.X < 212) && (e.Y > 1 && e.Y < 89))
        m_imageScore = 25;
    else if ((e.X > 2 && e.X < 87) && (e.Y > 121 && e.Y < 211))
        m_imageScore = 10;
    else if ((e.X > 124 && e.X < 212) && (e.Y > 94 && e.Y < 218))
        m_imageScore = 5;

}

//Finding score for Checkbox List
 public void CheckBoxList(object sender, EventArgs e)
 {
     foreach (ListItem item in CheckBoxlist1.Items)
         if (item.Selected)
         {
             m_score = Int32.Parse(item.Value) + m_score;
         }
 }

 //Finding score for the remaining lists
 public void ButtonClick1(object sender, EventArgs e)
 {
    m_score = textComments.Text.Length + m_score;
    m_score = Int32.Parse(DropDownList1.SelectedItem.Value) + m_score;
    m_score = Int32.Parse(RadioButtonList1.SelectedItem.Value) + m_score;
    m_score = Int32.Parse(workList.SelectedItem.Value) + m_score;

    int final_score = m_score + m_imageScore;
    if (final_score > 100)
        lblresults.Text = "You are an outgoing personality type.";
    else if (final_score > 100 && final_score <50)
        lblresults.Text = "You are a merry soul.";
    else if (final_score < 50 && final_score > 25)
        lblresults.Text = "You are a regular kind of all-around person.";
    else if (final_score <25 && final_score > 0)
        lblresults.Text = "You are a shy one, aren't you?";

 }








</script>

<html>
<head id="Head1" runat="server">
<title>Personality Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
    <asp:Label id="lblName"
        Text="Name"
        AssociatedControlID="txtName"
        runat="server" />

    <asp:TextBox
        id="txtname"
        AutoPostBack="true"
        runat="server" />

    <br /><br />

    <asp:TextBox
        id="textComments"
        Text="Tell me a little about yourself"
        TextMode="MultiLine"
        Columns="30"
        rows="10"
        runat="server" />
    <br /><br />

    <strong>Select a gender:</strong>
    <asp:RadioButton
        id="rd1Male"
        Text="Male"
        GroupName="rgGender"
        runat="server" />

    <asp:RadioButton
        id="rd1Female"
        Text="Female"
        GroupName="rgGender"
        runat="server" />
    <br /><br />


    <strong>Favorite Season:</strong>
    <br />
     <asp:DropDownList
         id="DropDownList1"
         Runat="server"
         AutoPostBack="true"
         >
    <asp:ListItem value="15">Spring</asp:ListItem>
    <asp:ListItem value="10">Summer</asp:ListItem>
    <asp:ListItem value="20">Fall</asp:ListItem>
    <asp:ListItem value="5">Winter</asp:ListItem>
    </asp:DropDownList>
    <br /><br />


    <strong>Which of the following colors are your favorite?</strong>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem Value="3">Red</asp:ListItem>
        <asp:ListItem Value="4">Blue</asp:ListItem>
        <asp:ListItem Value="7">Purple</asp:ListItem>
        <asp:ListItem Value="5">Yellow</asp:ListItem>
        <asp:ListItem Value="6">Green</asp:ListItem>
        <asp:ListItem Value="2">Orange</asp:ListItem>
        <asp:ListItem Value="8">Violet</asp:ListItem>
        <asp:ListItem Value="9">Pink</asp:ListItem>
        <asp:ListItem Value="10">Brown</asp:ListItem>
        <asp:ListItem Value="0">Grey</asp:ListItem>
    </asp:RadioButtonList>

    <br /><br />
    <strong>Which type of work do you prefer?</strong>
    <br />
      <asp:DropDownList
         id="workList"
         Runat="server"
         AutoPostBack="true"
         OnSelectedIndexChanged="workListChanged">
    <asp:ListItem Value="3">Office Work</asp:ListItem>
    <asp:ListItem value="2">Outdoor Work</asp:ListItem>
    <asp:ListItem value="7">Investigative Work</asp:ListItem>
    <asp:ListItem value="6">Woring With People</asp:ListItem>
    <asp:ListItem value="5">Work Requiring Travel</asp:ListItem>
    <asp:ListItem value="4">Helping Others</asp:ListItem>
    </asp:DropDownList>
    <br />

    <asp:Label
        id="lblWork"
        runat ="server" />
    <br /><br />


    <strong>Which foods do you like (check as many as you want)</strong>
    <asp:CheckBoxList 
    id="CheckBoxlist1" 
    runat="server" 
    RepeatColumns="6"
     RepeatDirection="Horizontal"
     TextAlign="Right">
    <asp:ListItem value="13">Bread</asp:ListItem>
    <asp:ListItem value="14">Carrots</asp:ListItem>
    <asp:ListItem value="1">Ice Cream</asp:ListItem>
    <asp:ListItem value="2">Potato Chips</asp:ListItem>
    <asp:ListItem value="5">Candy Bars</asp:ListItem>
    <asp:ListItem value="7">Cheesecake</asp:ListItem>
    <asp:ListItem value="16">Steak</asp:ListItem>
    <asp:ListItem value="9">Soft Drinks</asp:ListItem>
    <asp:ListItem value="15">Milk</asp:ListItem>
    <asp:ListItem value="11">Spaghetti</asp:ListItem>
    <asp:ListItem value="11">Pizza</asp:ListItem>
    <asp:ListItem value="17">Cheese</asp:ListItem>
    <asp:ListItem value="15">Almonds</asp:ListItem>
    <asp:ListItem value="3">Brownies</asp:ListItem>
    <asp:ListItem value="13">Apples</asp:ListItem>
    <asp:ListItem value="14">Oranges</asp:ListItem>
    <asp:ListItem value="15">Melons</asp:ListItem>
    <asp:ListItem value="14">Peas</asp:ListItem>
    <asp:ListItem value="19">Brussel Sprouts</asp:ListItem>
    <asp:ListItem value="15">Salads</asp:ListItem>
    <asp:ListItem value="14">Tuna Fish</asp:ListItem>
    </asp:CheckBoxList>
    <br /><br />
    <strong>Which picture most appeals to you?</strong>
    <br />
    <asp:ImageButton
        id="imgChoice"
        imageURL="PersTest.jpg"
        runat="server"
        OnClick="ImageButton_Click" />
    <br /><br /><br /><br />

    <asp:Button
        id="btnPersonality"
        Text="Submit Personality"
        runat="server" 
        OnClick="ButtonClick1"/>
    <br /><br />

    <asp:Label
        id="lblresults"
        runat="server" />



</div>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

请尝试使用开关,而不是使用if语句来确定所选项目。这将为您提供更多控制权,因为您可能会遇到多个案例并设置默认值。

我认为你的主要问题将来自你的得分。看看你的第二次得分检查:

else if (final_score > 100 && final_score <50)

注意什么?您正在测试它是否大于100且小于50.我认为您在这里有两个倒退。如果它等于100会发生什么?您的所有情况都大于或小于,但没有考虑到相同的匹配,因此您最终会遇到漏洞。如果它是50岁会怎么样?没有。如果它是0会怎么样?这些情况都没有。你可能想尝试类似的东西:

if (final_score > 100)
    lblresults.Text = "You are an outgoing personality type.";
else if (final_score <= 100 && final_score > 50)
    lblresults.Text = "You are a merry soul.";
else if (final_score <= 50 && final_score > 25)
    lblresults.Text = "You are a regular kind of all-around person.";
else if (final_score <= 25 && final_score >= 0)
    lblresults.Text = "You are a shy one, aren't you?";