在ASP.NET里实现计算器代码的封装
2015-11-26 21:02
441 查看
一、具体代码
Default2.aspx.cspublic partial class Chapter1_Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } int a = 0; private int t; public static int Count=0; public static int right=0; protected void Button1_Click(object sender, EventArgs e) { int a = int.Parse(TextBox1.Text.Trim()); int b = int.Parse(TextBox3.Text.Trim()); Char c = Convert.ToChar(TextBox2.Text.Trim()); Lei con = new Lei(); con.Max(a, b, c); if (con.Answer == int.Parse(TextBox4.Text.Trim())) { Label1.Text=("恭喜你,小朋友!回答正确!"); Label1.ForeColor=Color.Blue; right++; } else { Label1.Text = ("回答错误,继续加油!小朋友"); Label1.ForeColor = Color.Red; } } protected void Button2_Click(object sender, EventArgs e) { Count++; StreamWriter baocun1 = File.AppendText("C:\\baocun1.txt"); baocun1.WriteLine(TextBox1.Text); baocun1.Close(); StreamWriter baocun2 = File.AppendText("C:\\baocun2.txt"); baocun2.WriteLine(TextBox2.Text); baocun2.Close(); StreamWriter baocun3 = File.AppendText("C:\\baocun3.txt"); baocun3.WriteLine(TextBox3.Text); baocun3.Close(); ListBox1.Items.Add(TextBox1.Text + TextBox2.Text + TextBox3.Text + "="); TextBox1.Text = ""; TextBox2.Text = ""; TextBox3.Text = ""; } protected void Button3_Click(object sender, EventArgs e) { TextBox1.BackColor = Color.Yellow; TextBox2.BackColor = Color.Yellow; TextBox3.BackColor = Color.Yellow; TextBox4.BackColor = Color.Yellow; TextBox1.Enabled = false; TextBox2.Enabled = false; TextBox3.Enabled = false; string[] m = new string[100]; m = File.ReadAllLines("C:\\baocun1.txt"); TextBox1.Text = m[a]; string[] n = new string[100]; n = File.ReadAllLines("C:\\baocun2.txt"); TextBox2.Text = n[a]; string[] v = new string[100]; v = File.ReadAllLines("C:\\baocun3.txt"); TextBox3.Text = v[a]; a++; } protected void Button4_Click(object sender, EventArgs e) { TextBox1.Text = ""; TextBox2.Text = ""; TextBox3.Text = ""; TextBox4.Text = ""; Label1.Text = ""; } protected void Button5_Click(object sender, EventArgs e) { Label6.Text = "总计"; Label7.Text = "正确"; Label8.Text = "正确率"; Label3.Text = Count.ToString(); Label4.Text = right.ToString(); Label5.Text = ((right / (double)(Count)) * 100).ToString() + "%"; } }
封装代码:
类代码要写在App_Code里
public class Lei { public int Sum; public int Answer { get { return Sum; } } public int Max(int n1,int n2,char yunsuanfu) { if (yunsuanfu == '+') { return Sum = n1 + n2; } else if (yunsuanfu == '-') { return Sum = n1 - n2; } else if (yunsuanfu == '/') { return Sum = n1 / n2; } else if (yunsuanfu == '*') { return Sum = n1 * n2; } return Sum; } }
二、测试
相关文章推荐
- ASP.NET多个提交按钮页面,回车Enter执行指定按钮的事件
- Asp.net默认配置下,Session莫名丢失的原因及解决
- ASP.NET根据URL生成网页缩略图示例程序(C#语言)
- asp.net计算器(策略模式)
- asp.net Cookie 中文乱码
- ASP.NET MVC 中的IResolver<T> 接口
- asp.net发布到IIS后提示ORA-12154:TNS:无法解析指定的连接标识符
- asp.net web开发中使用JS百度地图信息弹出窗中显示echarts图
- 封装,策略模式,Asp换脸
- ASP.NET 页生命周期概述
- Asp.Net bundles 404
- VASP赝势文件POTCAR 解析
- asp.net(C#)读取文件夹和子文件夹下所有文件,绑定到GRIDVIEW并排序 .
- 用VASP计算GW+BSE
- 还在为身份验证引入的Microsoft.AspNet.Identity.EntityFramework导致多上下文,生成的DB改名困扰吗?
- ASP.NET动态创建数据库和表
- ASP.NET MVC ,WebAPI 自定义Basic授权方式
- asp.net mvc AjaxBeginForm结合Partial使用
- ASP.NET 5和MVC 几大变化
- win7部署.net4.0开发的asp.net出错SimpleHandlerFactory-Integrated