zoukankan      html  css  js  c++  java
  • 策略模式,ASP.NET实现

    策略模式,ASP.NET实现

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5 
     6 /// <summary>
     7 ///Class1 的摘要说明
     8 /// </summary>
     9 interface  Iface
    10 {
    11     int Calation(int a, int b);
    12     
    13 }
    14 public class Add:Iface
    15 {
    16     public int Calation(int a,int b)
    17     {
    18         return a+b;
    19     }
    20 }
    21 public class Sub : Iface
    22 {
    23     public int Calation(int a, int b)
    24     {
    25         return a - b;
    26     }
    27 }
    28 public class Mul : Iface
    29 {
    30     public int Calation(int a, int b)
    31     {
    32         return a * b;
    33     }
    34 }
    35 public class Div : Iface
    36 {
    37     public int Calation(int a, int b)
    38     {
    39         if (b == 0)
    40         {
    41             throw new Exception("除数不能为零!");
    42 
    43         }
    44 
    45         else
    46         {
    47             return a / b;
    48         }
    49         
    50     }
    51 }
    52 public class Faction
    53 {
    54     private Iface iface;
    55     public Faction(string operation)
    56     {
    57         switch (operation)
    58         {
    59             case "+":
    60                 iface = new Add();
    61                 break;
    62             case "-":
    63                 iface = new Sub();
    64                 break;
    65             case "*":
    66                 iface = new Mul();
    67                 break;
    68             case "/":
    69                 iface = new Div();
    70                 break;
    71         }
    72     }
    73     public int Calationss(int a, int b)
    74     {
    75         return iface.Calation(a, b);
    76  
    77     }
    78 }

    Default.aspx

    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)
            {
                ti();
            }
        }
        public void ti()
        {
            Random rd = new Random();
            TextBox1.Text = rd.Next(1, 100).ToString();
            TextBox2.Text = rd.Next(1, 100).ToString();
            string[] oper = new string[] { "+", "-", "*", "/" };
            Random rdd = new Random();
            Label1.Text = oper[rdd.Next(1, 4)];
     
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Faction faction = new Faction(Label1.Text);
            int a = int.Parse(TextBox1.Text);
            int b = int.Parse(TextBox2.Text);
            string anster = faction.Calationss(a, b).ToString();
            if (TextBox3.Text == anster)
            {
                Response.Write("回答正确!");
            }
            else
            {
                Response.Write("回答错误!");
            }
            TextBox3.Text = "";
            ti();
        }
    }

    测试

    第一题做的是不是正确,在第二题中提示。

  • 相关阅读:
    Kubernetes节点维护
    Kubernetes helm配置国内镜像源
    windows universal app中使用mvvm light
    windows phone 开发常用小技巧
    异步编程中的最佳做法(Async/Await) --转
    windows phone 开发常用小技巧
    windows phone 开发常用小技巧
    windows phone 开发常用小技巧
    #假期归来# 看看国外开发者第一时间用swift写的Flappy Bird (2014.6.3)
    vs2013 TFS如何彻底删除团队项目
  • 原文地址:https://www.cnblogs.com/zwt0626/p/5044594.html
Copyright © 2011-2022 走看看