转asp.net C#简单计算器
一个简单的计算器,如图:
前台代码如下:
<div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem>选择</asp:ListItem> <asp:ListItem>+</asp:ListItem> <asp:ListItem>-</asp:ListItem> <asp:ListItem>*</asp:ListItem> <asp:ListItem>/</asp:ListItem> <asp:ListItem>%</asp:ListItem> </asp:DropDownList> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>=<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> </div>
后台代码:
protected void Button1_Click(object sender, EventArgs e)
{
double a = Convert.ToDouble(TextBox1.Text);
double b = Convert.ToDouble(TextBox2.Text);
string c = DropDownList1.Text;
double d = 0;
d = Class1.Switch(a, b, c, d);
TextBox3.Text = Convert.ToString(d);
}
调用Class1类中的Switch 方法:
public class Class1
{
//四则运算
public static double Switch(double a, double b, string c, double d)
{
switch (c)
{
case "+":
d = a + b;
break;
case "-":
d = a - b;
break;
case "*":
d = a * b;
break;
case "/":
d = a / b;
break;
case "%":
d = a % b;
break;
default:
break;
}
return d;
}
}