calation.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> ///Calation 的摘要说明 /// </summary> public class Calation { public Calation() { // //TODO: 在此处添加构造函数逻辑 // } } interface ODD { int members(int a, int b); } class Class2 : ODD { public int members(int a, int b) { return a + b; } } class sub : ODD { public int members(int a, int b) { return a - b; } } class Mul : ODD { public int members(int a, int b) { return a * b; } } class Div : ODD { public int members(int a, int b) { return a / b; } } public class suanfa { private ODD jisuan; public void math(string TR) { switch (TR) { case "+": jisuan = new Class2(); break; case "-": jisuan = new sub(); break; case "*": jisuan = new Mul(); break; case "/": jisuan = new Div(); break; } } public int clation(int a, int b) { return jisuan.members(a, b); } }
Default.aspx.cs
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 { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { rando(); } } public void rando() { Random an = new Random(); int a=an .Next(0,100); int b=an.Next(0,100); TextBox1.Text = a.ToString(); TextBox3.Text = b.ToString(); string [] ann=new string[]{"+","-","*","/"}; TextBox2.Text = ann[new Random().Next(0, 4)]; } protected void Button1_Click(object sender, EventArgs e) { int a = Convert.ToInt32(TextBox1.Text); int b = Convert.ToInt32(TextBox3.Text); suanfa suanfa1 = new suanfa(); suanfa1.math(TextBox2.Text); int answer = suanfa1.clation(a, b); if (answer.ToString() == TextBox4.Text) { Response.Write("回答正确!"); } else { Response.Write("回答错误!"); Response.Write(answer.ToString()); } TextBox4.Text = ""; rando(); } }
测试截图:
虽然界面简单 但还是用asp实现出来了。。
时间: 2024-10-13 04:59:29