ASP.net之策略模式
生活随笔
收集整理的這篇文章主要介紹了
ASP.net之策略模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
設計思路:
用ASP.net設計,調用策略模式。在第一個數和第二個數的文本框中輸入數值,單擊錄題按鈕,數值保存在n1,n2文檔中,把要做的題都保存完后,單擊開始按鈕,開始做題,做完單擊判斷按鈕,進行判斷,進入下一題,同時提示回答是否正確。如果在時間內做完題就單擊結束按鈕,彈出對話框“答題結束” 總計,正確的個數及正確率顯示出來。
頁面設計代碼如下:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using System.IO; 8 9 public partial class one : System.Web.UI.Page 10 { 11 protected void Page_Load(object sender, EventArgs e) 12 { 13 14 } 15 public static int Count = 0;//總計的個數 16 public static int right = 0;//正確的個數 17 int n = 0; 18 19 protected void Button8_Click(object sender, EventArgs e) 20 { 21 fh.Text="+"; 22 fh.Visible = true; 23 } 24 protected void Button9_Click(object sender, EventArgs e) 25 { 26 fh.Text = "-"; 27 fh.Visible = true; 28 } 29 protected void Button10_Click(object sender, EventArgs e) 30 { 31 fh.Text = "*"; 32 fh.Visible = true; 33 } 34 protected void Button11_Click(object sender, EventArgs e) 35 { 36 fh.Text = "/"; 37 fh.Visible = true; 38 } 39 //以上四個分別是+,-,*,/的單擊事件 40 protected void luti_Click(object sender, EventArgs e)//錄題事件 41 { 42 StreamWriter n1 = File.AppendText("n1.txt");//第一個數存入第一文檔 43 n1.WriteLine(No1.Text); 44 n1.Close(); 45 StreamWriter n2 = File.AppendText("n2.txt");//第二個數存入第二個文檔 46 n2.WriteLine(fh.Text); 47 n2.Close(); 48 StreamWriter n3 = File.AppendText("n3.txt");//結果存入第三個文檔 49 n3.WriteLine(No2.Text); 50 n3.Close(); 51 No1.Text = "";//清空數一,數二,結果 52 No2.Text=""; 53 No3.Text=""; 54 Response.Write("錄題成功"); 55 } 56 protected void ready_Click(object sender, EventArgs e)//開始的單擊事件 57 { 58 59 string[] n1 = new string[100]; 60 n1 = File.ReadAllLines("n1.txt");//數值一的文檔 61 No1.Text = n1[n]; 62 string[] n2 = new string[100]; 63 n2 = File.ReadAllLines("n2.txt"); 64 fh.Text = n2[n]; 65 string[] n3 = new string[100]; 66 n3 = File.ReadAllLines("n3.txt"); 67 No2.Text = n3[n]; 68 n++; 69 70 } 71 protected void over_Click(object sender, EventArgs e) 72 { 73 74 No3.Enabled = false; 75 Response.Write("運算結束!"); 76 all.Text = Count.ToString();//題目總數 77 rt.Text = right.ToString();//正確的個數 78 rt1.Text = ((right / (double)(Count)) * 100).ToString() + "%";//正確率 79 } 80 protected void ifright_Click(object sender, EventArgs e) 81 { 82 Class1.mark mark = null; 83 double a = Convert.ToDouble(No1.Text);//第一個數賦值 84 double b = Convert.ToDouble(No2.Text);//第二個數賦值 85 string c = fh.Text;//運算符號 86 switch (c) 87 { 88 case "+": 89 mark = new Class1.mark(new Class1.add());//調用策略模式 90 break; 91 case "-": 92 mark = new Class1.mark(new Class1.sub()); 93 break; 94 case "*": 95 mark = new Class1.mark(new Class1.mul()); 96 break; 97 case "/": 98 mark = new Class1.mark(new Class1.div()); 99 break; 100 101 default: 102 break; 103 } 104 string result = mark.Call(a, b,c).ToString(); 105 if (No3.Text == result.ToString()) 106 { 107 Response.Write("回答正確!下一題請按開始按鈕!"); 108 right++; 109 Count++; 110 } 111 112 else 113 { 114 115 Response.Write("回答錯誤!下一題請按開始按鈕!"); 116 Count++; 117 118 } 119 //清空 120 No3.Style.Clear(); 121 } 122 }策略模式代碼:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 /// <summary> 7 ///Class1 的摘要說明 8 /// </summary> 9 public class Class1 10 { 11 12 //定義一個接口 13 public interface Calculator 14 { 15 double Call(double a, double b);//定義一個方法用于計算 16 } 17 private double a; 18 private double b;//定義變量 19 public class add : Calculator 20 { 21 public double Call(double a, double b) 22 { 23 double result; 24 result = a + b; 25 return result; 26 } 27 } 28 public class sub : Calculator 29 { 30 public double Call(double a, double b) 31 { 32 double result; 33 result = a - b; 34 return result; 35 } 36 } 37 public class mul : Calculator 38 { 39 public double Call(double a, double b) 40 { 41 double result; 42 result = a * b; 43 return result; 44 } 45 } 46 public class div : Calculator 47 { 48 public double Call(double a, double b) 49 { 50 double result; 51 result = a / b; 52 return result; 53 } 54 } 55 public class mark 56 { 57 private Calculator calculate; 58 59 public mark(Calculator calculate) 60 { 61 this.calculate = calculate; 62 } 63 public double Call(double a, double b, string m) 64 { 65 return this.calculate.Call(a, b); 66 } 67 } 68 }運行后的圖片:
轉載于:https://www.cnblogs.com/xueyanan/p/5059016.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的ASP.net之策略模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【SSH】——Hibernate三种状态
- 下一篇: 信息安全系统设计基础第十五周总结