大大大摆锤 2022-01-01 15:33 采纳率: 100%
浏览 44
已结题

c#计算后在网页上输出结果

相关代码,设计一个程序,计算后在网页上输出结果

  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-01-01 15:42
    关注

    计算器?这种直接前端js就能实现,不需要经过服务器,参考:https://c.runoob.com/codedemo/3654/
    一定要C#,webform简单示例如下

    img

    x.aspx

     <%@ Page Language="C#" AutoEventWireup="true"%>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack) {
                if (a.Text != "" && b.Text != "" && opr.Text != "")
                {
                    double da = 0, db = 0, dc = 0;
                    double.TryParse(a.Text, out da);
                    double.TryParse(b.Text, out db);
                    switch (opr.Text)
                    {
                        case "+": dc = da + db; break;
                        case "-": dc = da - db; break;
                        case "*": dc = da * db; break;
                        case "/": dc = da / db; break;
                    }
                    c.Text = Math.Round(dc, 2).ToString();
                }
                else c.Text = "";
            }
        }
    </script>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Asp.Net计算器</title>
    </head>
    <body>
        <form id="form1" runat="server">
           <asp:TextBox runat="server" ID="a"></asp:TextBox>
            <asp:DropDownList ID="opr" runat="server" AutoPostBack="true">
                <asp:ListItem Value="">请选择操作</asp:ListItem>
                <asp:ListItem Value="+">+</asp:ListItem>
                <asp:ListItem Value="-">-</asp:ListItem>
                <asp:ListItem Value="*">*</asp:ListItem>
                <asp:ListItem Value="/">/</asp:ListItem>
            </asp:DropDownList>
           <asp:TextBox runat="server" ID="b"></asp:TextBox>
            =
           <asp:TextBox runat="server" ID="c"></asp:TextBox>
        </form>
    </body>
    </html>
     
    
    
    

    img


    有其他问题可以继续交流~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月3日
  • 已采纳回答 1月3日
  • 创建了问题 1月1日