ASP学习之基于Wizard控件的乘法测试程序

1.关于乘法测试程序

程序随机生成5道三位数乘法和4个顺序随机打乱的答案(其中1个答案是正确答案),并根据用户选择进行计分(每题20分)。

第一阶段:欢迎-输入用户名

211025_7mGc_1425762.png

第二阶段:5道三位数乘法题

211025_DbAw_1425762.png

(中间步骤略)

最后阶段:进行判分

211026_9YyQ_1425762.png

2.ASP代码部分

body标签内代码

<body>
    <form id="form1" runat="server">
        <asp:Wizard ID="wzdQA" runat="server" ActiveStepIndex="0" Height="200px" Width="460px"
            OnActiveStepChanged="wzdQA_ActiveStepChanged" 
            OnFinishButtonClick="wzdQA_FinishButtonClick" 
            FinishCompleteButtonText="重新开始">
            <SideBarStyle BackColor="#99FF99" BorderColor="#3399FF" Width="100px" 
                BorderWidth="2px" BorderStyle="Solid" 
                HorizontalAlign="Center" VerticalAlign="Middle" />
            <WizardSteps>
                <asp:WizardStep runat ="server" ID="wzdsStep0" 
                    Title ="欢迎" StepType="Start">
                    输入一个名字开始征程吧
                    <asp:TextBox runat="server" ID="txtName" Text="北风其凉Tsybius">
                    </asp:TextBox>
                </asp:WizardStep>
                <asp:WizardStep runat ="server" ID="wzdsStep1" 
                    Title ="第一题" StepType="Step">
                    <asp:Label runat="server" ID="lbQuestion1" Text="Question1" />
                    <hr />
                    <asp:RadioButton runat="server" ID="rdbQ1A1" Text="Q1A1" GroupName="Q1" />
                    <asp:RadioButton runat="server" ID="rdbQ1A2" Text="Q1A2" GroupName="Q1" />
                    <asp:RadioButton runat="server" ID="rdbQ1A3" Text="Q1A3" GroupName="Q1" />
                    <asp:RadioButton runat="server" ID="rdbQ1A4" Text="Q1A4" GroupName="Q1" />
                </asp:WizardStep>
                <asp:WizardStep runat ="server" ID="wzdsStep2" 
                    Title ="第二题" StepType="Step">
                    <asp:Label runat="server" ID="lbQuestion2" Text="Question2" />
                    <hr />
                    <asp:RadioButton runat="server" ID="rdbQ2A1" Text="Q2A1" GroupName="Q2" />
                    <asp:RadioButton runat="server" ID="rdbQ2A2" Text="Q2A2" GroupName="Q2" />
                    <asp:RadioButton runat="server" ID="rdbQ2A3" Text="Q2A3" GroupName="Q2" />
                    <asp:RadioButton runat="server" ID="rdbQ2A4" Text="Q2A4" GroupName="Q2" />
                </asp:WizardStep>
                <asp:WizardStep runat ="server" ID="wzdsStep3" 
                    Title ="第三题" StepType="Step">
                    <asp:Label runat="server" ID="lbQuestion3" Text="Question3" />
                    <hr />
                    <asp:RadioButton runat="server" ID="rdbQ3A1" Text="Q3A1" GroupName="Q3" />
                    <asp:RadioButton runat="server" ID="rdbQ3A2" Text="Q3A2" GroupName="Q3" />
                    <asp:RadioButton runat="server" ID="rdbQ3A3" Text="Q3A3" GroupName="Q3" />
                    <asp:RadioButton runat="server" ID="rdbQ3A4" Text="Q3A4" GroupName="Q3" />
                </asp:WizardStep>
                <asp:WizardStep runat ="server" ID="wzdsStep4" 
                    Title ="第四题" StepType="Step">
                    <asp:Label runat="server" ID="lbQuestion4" Text="Question4" />
                    <hr />
                    <asp:RadioButton runat="server" ID="rdbQ4A1" Text="Q4A1" GroupName="Q4" />
                    <asp:RadioButton runat="server" ID="rdbQ4A2" Text="Q4A2" GroupName="Q4" />
                    <asp:RadioButton runat="server" ID="rdbQ4A3" Text="Q4A3" GroupName="Q4" />
                    <asp:RadioButton runat="server" ID="rdbQ4A4" Text="Q4A4" GroupName="Q4" />
                </asp:WizardStep>
                <asp:WizardStep runat ="server" ID="wzdsStep5" 
                    Title ="第五题" StepType="Step">
                    <asp:Label runat="server" ID="lbQuestion5" Text="Question5" />
                    <hr />
                    <asp:RadioButton runat="server" ID="rdbQ5A1" Text="Q5A1" GroupName="Q5" />
                    <asp:RadioButton runat="server" ID="rdbQ5A2" Text="Q5A2" GroupName="Q5" />
                    <asp:RadioButton runat="server" ID="rdbQ5A3" Text="Q5A3" GroupName="Q5" />
                    <asp:RadioButton runat="server" ID="rdbQ5A4" Text="Q5A4" GroupName="Q5" />
                </asp:WizardStep>
                <asp:WizardStep runat="server" ID="wzdsStepLast" 
                    Title="结束" StepType="Finish">
                    <b><asp:Label runat="server" ID="lblMark" Text="Label"></asp:Label></b>
                </asp:WizardStep>
            </WizardSteps>
       </asp:Wizard>
  </form>
</body>

3.C#代码部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //随机数
    static Random rand = new Random();

    //问题类
    private class QA
    {
        //问题、答案1-4、正确答案
        public string Question, Answer1, Answer2, Answer3, Answer4, CorrectAnswer;
        /// <summary>
        /// 创建一个问题
        /// </summary>
        /// <param name="ques">问题</param>
        /// <param name="ans">四个备选答案(构造问题时会打乱顺序)</param>
        /// <param name="corrans">正确答案</param>
        public QA(string ques, string[] ans, string corrans)
        {
            if (ans.Length != 4)
            {
                throw new Exception("答案设置不合法");
            }

            //问题与正确答案
            Question = ques;
            CorrectAnswer = corrans;

            //四个备选答案的顺序
            string[] order = new string[] 
            { 
                "0123", "0132", "0213", "0231", "0312", "0321",
                "1023", "1032", "1203", "1230", "1302", "1320",
                "2013", "2031", "2103", "2130", "2301", "2310",
                "3012", "3021", "3102", "3120", "3201", "3210",
            };

            int iChosenOrder = (int)(rand.Next(0, 24));
            Answer1 = ans[int.Parse(order[iChosenOrder][0].ToString())];
            Answer2 = ans[int.Parse(order[iChosenOrder][1].ToString())];
            Answer3 = ans[int.Parse(order[iChosenOrder][2].ToString())];
            Answer4 = ans[int.Parse(order[iChosenOrder][3].ToString())];
        }
    }

    //必须要声明static否则Wizard控件翻页后无法改变值
    private static QA[] qa = new QA[5];

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    //Wizard控件变化页面,在首页时生成一组题目,在末页时对答题进行判分
    protected void wzdQA_ActiveStepChanged(object sender, EventArgs e)
    {
        switch (wzdQA.ActiveStep.ID)
        {
            case "wzdsStep0":
                {
                    #region 生成题目
                    int a, b;
                    string ans1, ans2, ans3, ans4;

                    #region 第一题

                    a = rand.Next(100, 1000);
                    b = rand.Next(100, 1000);

                    //答案1
                    ans1 = (a * b).ToString();
                    //答案2
                    do
                    {
                        ans2 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans2 == ans1);
                    //答案3
                    do
                    {
                        ans3 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans3 == ans1 || ans3 == ans2);
                    //答案4
                    do
                    {
                        ans4 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans4 == ans1 || ans4 == ans2 || ans4 == ans3);

                    qa[0] = new QA(
                        a.ToString() + "*" + b.ToString() + "=?", //题目表述
                        new string[] { ans1, ans2, ans3, ans4 },  //答案1-4
                        (a * b).ToString());//正确答案
                    lbQuestion1.Text = qa[0].Question;
                    rdbQ1A1.Text = qa[0].Answer1;
                    rdbQ1A2.Text = qa[0].Answer2;
                    rdbQ1A3.Text = qa[0].Answer3;
                    rdbQ1A4.Text = qa[0].Answer4;

                    #endregion
                    #region 第二题

                    a = rand.Next(100, 1000);
                    b = rand.Next(100, 1000);

                    //答案1
                    ans1 = (a * b).ToString();
                    //答案2
                    do
                    {
                        ans2 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans2 == ans1);
                    //答案3
                    do
                    {
                        ans3 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans3 == ans1 || ans3 == ans2);
                    //答案4
                    do
                    {
                        ans4 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans4 == ans1 || ans4 == ans2 || ans4 == ans3);

                    qa[1] = new QA(
                        a.ToString() + "*" + b.ToString() + "=?", //题目表述
                        new string[] { ans1, ans2, ans3, ans4 },  //答案1-4
                        (a * b).ToString());     //正确答案
                    lbQuestion2.Text = qa[1].Question;
                    rdbQ2A1.Text = qa[1].Answer1;
                    rdbQ2A2.Text = qa[1].Answer2;
                    rdbQ2A3.Text = qa[1].Answer3;
                    rdbQ2A4.Text = qa[1].Answer4;

                    #endregion
                    #region 第三题

                    a = rand.Next(100, 1000);
                    b = rand.Next(100, 1000);

                    //答案1
                    ans1 = (a * b).ToString();
                    //答案2
                    do
                    {
                        ans2 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans2 == ans1);
                    //答案3
                    do
                    {
                        ans3 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans3 == ans1 || ans3 == ans2);
                    //答案4
                    do
                    {
                        ans4 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans4 == ans1 || ans4 == ans2 || ans4 == ans3);

                    qa[2] = new QA(
                        a.ToString() + "*" + b.ToString() + "=?", //题目表述
                        new string[] { ans1, ans2, ans3, ans4 },  //答案1-4
                        (a * b).ToString());     //正确答案
                    lbQuestion3.Text = qa[2].Question;
                    rdbQ3A1.Text = qa[2].Answer1;
                    rdbQ3A2.Text = qa[2].Answer2;
                    rdbQ3A3.Text = qa[2].Answer3;
                    rdbQ3A4.Text = qa[2].Answer4;

                    #endregion
                    #region 第四题

                    a = rand.Next(100, 1000);
                    b = rand.Next(100, 1000);

                    //答案1
                    ans1 = (a * b).ToString();
                    //答案2
                    do
                    {
                        ans2 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans2 == ans1);
                    //答案3
                    do
                    {
                        ans3 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans3 == ans1 || ans3 == ans2);
                    //答案4
                    do
                    {
                        ans4 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans4 == ans1 || ans4 == ans2 || ans4 == ans3);

                    qa[3] = new QA(
                        a.ToString() + "*" + b.ToString() + "=?", //题目表述
                        new string[] { ans1, ans2, ans3, ans4 },  //答案1-4
                        (a * b).ToString());     //正确答案
                    lbQuestion4.Text = qa[3].Question;
                    rdbQ4A1.Text = qa[3].Answer1;
                    rdbQ4A2.Text = qa[3].Answer2;
                    rdbQ4A3.Text = qa[3].Answer3;
                    rdbQ4A4.Text = qa[3].Answer4;

                    #endregion
                    #region 第五题

                    a = rand.Next(100, 1000);
                    b = rand.Next(100, 1000);

                    //答案1
                    ans1 = (a * b).ToString();
                    //答案2
                    do
                    {
                        ans2 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans2 == ans1);
                    //答案3
                    do
                    {
                        ans3 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans3 == ans1 || ans3 == ans2);
                    //答案4
                    do
                    {
                        ans4 = (a * b + 10 * rand.Next(-10, 10)).ToString();
                    } while (ans4 == ans1 || ans4 == ans2 || ans4 == ans3);

                    qa[4] = new QA(
                        a.ToString() + "*" + b.ToString() + "=?", //题目表述
                        new string[] { ans1, ans2, ans3, ans4 },  //答案1-4
                        (a * b).ToString());     //正确答案
                    lbQuestion5.Text = qa[4].Question;
                    rdbQ5A1.Text = qa[4].Answer1;
                    rdbQ5A2.Text = qa[4].Answer2;
                    rdbQ5A3.Text = qa[4].Answer3;
                    rdbQ5A4.Text = qa[4].Answer4;

                    #endregion
                    #endregion
                } break;
            case "wzdsStepLast":
                {
                    #region 计算得分
                    int counter = 0;

                    //第一题
                    if (rdbQ1A1.Checked && rdbQ1A1.Text == qa[0].CorrectAnswer) counter += 20;
                    if (rdbQ1A2.Checked && rdbQ1A2.Text == qa[0].CorrectAnswer) counter += 20;
                    if (rdbQ1A3.Checked && rdbQ1A3.Text == qa[0].CorrectAnswer) counter += 20;
                    if (rdbQ1A4.Checked && rdbQ1A4.Text == qa[0].CorrectAnswer) counter += 20;

                    //第二题
                    if (rdbQ2A1.Checked && rdbQ2A1.Text == qa[1].CorrectAnswer) counter += 20;
                    if (rdbQ2A2.Checked && rdbQ2A2.Text == qa[1].CorrectAnswer) counter += 20;
                    if (rdbQ2A3.Checked && rdbQ2A3.Text == qa[1].CorrectAnswer) counter += 20;
                    if (rdbQ2A4.Checked && rdbQ2A4.Text == qa[1].CorrectAnswer) counter += 20;

                    //第三题
                    if (rdbQ3A1.Checked && rdbQ3A1.Text == qa[2].CorrectAnswer) counter += 20;
                    if (rdbQ3A2.Checked && rdbQ3A2.Text == qa[2].CorrectAnswer) counter += 20;
                    if (rdbQ3A3.Checked && rdbQ3A3.Text == qa[2].CorrectAnswer) counter += 20;
                    if (rdbQ3A4.Checked && rdbQ3A4.Text == qa[2].CorrectAnswer) counter += 20;

                    //第四题
                    if (rdbQ4A1.Checked && rdbQ4A1.Text == qa[3].CorrectAnswer) counter += 20;
                    if (rdbQ4A2.Checked && rdbQ4A2.Text == qa[3].CorrectAnswer) counter += 20;
                    if (rdbQ4A3.Checked && rdbQ4A3.Text == qa[3].CorrectAnswer) counter += 20;
                    if (rdbQ4A4.Checked && rdbQ4A4.Text == qa[3].CorrectAnswer) counter += 20;

                    //第五题
                    if (rdbQ5A1.Checked && rdbQ5A1.Text == qa[4].CorrectAnswer) counter += 20;
                    if (rdbQ5A2.Checked && rdbQ5A2.Text == qa[4].CorrectAnswer) counter += 20;
                    if (rdbQ5A3.Checked && rdbQ5A3.Text == qa[4].CorrectAnswer) counter += 20;
                    if (rdbQ5A4.Checked && rdbQ5A4.Text == qa[4].CorrectAnswer) counter += 20;

                    //输出成绩
                    lblMark.Text = txtName.Text + " 的成绩为:" + counter.ToString() + "(" +
                        (
                            counter < 60 ? "不及格" : (
                                counter == 60 ? "及格" : (
                                    counter == 80 ? "良好" : (
                                        counter == 100 ? "优秀" : ""
                        )))) + ")";

                    #endregion
                } break;
            default: break;
        }
    }

    //完成按钮,回到第一步,相当于重新开始一轮游戏
    protected void wzdQA_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        wzdQA.ActiveStepIndex = 0;
    }
}

END

转载于:https://my.oschina.net/Tsybius2014/blog/262446

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值