Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 计算器代码,如何在不按等于(=)键的情况下获得答案_Html_Asp.net - Fatal编程技术网

Html 计算器代码,如何在不按等于(=)键的情况下获得答案

Html 计算器代码,如何在不按等于(=)键的情况下获得答案,html,asp.net,Html,Asp.net,我的计算器代码在工作。我可以按5+9=14,但我想在不按=按钮的情况下做5+9+5+2=21。我试图在输入的所有操作数的末尾得到答案,而不是仅仅5+9。这是到目前为止我的代码 Public Class Already Inherits System.Web.UI.Page Dim total2 As Double Dim answer As Double Dim runningTotal As Double Dim sign As String

我的计算器代码在工作。我可以按5+9=14,但我想在不按=按钮的情况下做5+9+5+2=21。我试图在输入的所有操作数的末尾得到答案,而不是仅仅5+9。这是到目前为止我的代码

Public Class Already

    Inherits System.Web.UI.Page

    Dim total2 As Double
    Dim answer As Double
    Dim runningTotal As Double 
    Dim sign As String

    Private Sub Form_Load()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

     ' Code for the numbers                                               '
    Protected Sub btnZero_Click(sender As Object, e As EventArgs) Handles btnZero.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "0"
    End Sub

    Protected Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "1"
    End Sub

    Protected Sub btnTwo_Click(sender As Object, e As EventArgs) Handles btnTwo.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "2"
    End Sub

    Protected Sub btnThree_Click(sender As Object, e As EventArgs) Handles btnThree.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "3"
    End Sub

    Protected Sub btnFour_Click(sender As Object, e As EventArgs) Handles btnFour.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "4"
    End Sub

    Protected Sub btnFive_Click(sender As Object, e As EventArgs) Handles btnFive.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "5"
    End Sub

    Protected Sub btnSix_Click(sender As Object, e As EventArgs) Handles btnSix.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "6"
    End Sub

    Protected Sub btnSeven_Click(sender As Object, e As EventArgs) Handles btnSeven.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "7"
    End Sub

    Protected Sub btnEight_Click(sender As Object, e As EventArgs) Handles btnEight.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "8"
    End Sub

    Protected Sub btnNine_Click(sender As Object, e As EventArgs) Handles btnNine.Click
        If txtDisplay.Text = "0" Then txtDisplay.Text = ""
        txtDisplay.Text = txtDisplay.Text + "9"
    End Sub

    Protected Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        txtDisplay.Text = "0"
        Label1.Text = ""
        Label2.Text = ""
        Label3.Text = ""
        Label4.Text = ""
        txtDisplay2.Text = ""
        lblDisplay.Text = "0"
        lblDisplay.Text = ""
        lblSuperScript.Text = ""
    End Sub

     'Code for the plus button                                    '
    Protected Sub btnPlus_Click(sender As Object, e As EventArgs) Handles btnPlus.Click
        Label1.Text = txtDisplay.Text
       Label2.Text = "+"        
        txtDisplay.Text = ""
    End Sub

    'Code for the equals button                              '
    Protected Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click

        Label3.Text = txtDisplay.Text
        runningTotal = Label1.Text
        sign = Label2.Text
        total2 = Label3.Text


        Select Case (sign)
            Case Is = "+"
                answer = runningTotal + total2
            Case "-"
                answer = runningTotal - total2
            Case "*"
                answer = runningTotal * total2
            Case "/"
                answer = runningTotal / total2

        End Select 
       txtDisplay.Text = answer.ToString()
        runningTotal = answer
    End Sub

    Protected Sub btnBackSpace_Click(sender As Object, e As EventArgs) Handles btnBackSpace.Click
        If txtDisplay.Text.Length > 1 Then
            txtDisplay.Text = Mid(txtDisplay.Text, 1, Len(txtDisplay.Text) - 1)
        ElseIf txtDisplay.Text.Length = 1 Then
            txtDisplay.Text = 0
        Else
            Exit Sub
        End If
    End Sub

End Class
试试这个

ASPX

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Calculator.aspx.vb" Inherits="Calculator" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">

    <asp:ScriptManager runat="server" />

    <asp:UpdatePanel runat="server">
        <ContentTemplate>
            <p>
                <asp:Label ID="EquationLabel" runat="server" Text="&nbsp;" />
            </p>
            <p>
                <asp:Label ID="ResultLabel" runat="server" Text="0" />
            </p>
            <table>
            <tr>
                <td></td>
                <td><asp:Button runat="server" Text="CE" OnClick="back_click" /></td>
                <td><asp:Button runat="server" Text="C" OnClick="clear_click" /></td>
                <td><asp:Button runat="server" Text="=" OnClick="equal_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="7" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="8" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="9" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="/" OnClick="operand_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="4" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="5" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="6" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="*" OnClick="operand_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="1" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="2" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="3" OnClick="number_click" /></td>
                <td><asp:Button runat="server" Text="-" OnClick="operand_click" /></td>
            </tr>
            <tr>
                <td><asp:Button runat="server" Text="0" OnClick="number_click" /></td>
                <td></td>
                <td></td>
                <td><asp:Button runat="server" Text="+" OnClick="operand_click" /></td>
            </tr>
            </table>

        </ContentTemplate>
    </asp:UpdatePanel>

    </form>
</body>
</html>


VB代码隐藏

Option Explicit On
Option Strict On

Partial Class Calculator
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' Store List of items added in viewstate (numbers and operands)
    ''' </summary>
    Private Property EquationItems As List(Of Object)
        Get
            If ViewState("EquationItems") Is Nothing Then Return New List(Of Object)

            Return CType(ViewState("EquationItems"), List(Of Object))
        End Get
        Set(value As List(Of Object))
            ViewState("EquationItems") = value
        End Set
    End Property

    ''' <summary>
    ''' refersh equation and result
    ''' </summary>
    Private Sub Update()

        Dim Equation = ""
        Dim Result As Double = 0
        For i As Integer = 0 To EquationItems.Count - 1
            Dim item = EquationItems(i)
            Equation += item.ToString & " "

            Dim Num As Integer = If(TypeOf item Is Integer, CInt(item), -1)

            If i = 0 Then
                Result = If(Num > -1, Num, 0)
            Else
                If Num > -1 Then
                    Dim LastOperand = EquationItems(i - 1)
                    Result = Calc(Result, CStr(LastOperand), Num)
                End If
            End If
        Next

        EquationLabel.Text = Equation
        ResultLabel.Text = Result.ToString
    End Sub

    ''' <summary>
    ''' calaculate for each operand
    ''' </summary>
    Private Function Calc(Result As Double, Operand As String, Num As Integer) As Double
        If Operand = "+" Then
            Return Result + Num
        ElseIf Operand = "-" Then
            Return Result - Num
        ElseIf Operand = "*" Then
            Return Result * Num
        ElseIf Operand = "/" Then
            Return If(Num <> 0, Result / Num, 0)
        End If

        Return 0
    End Function

    ''' <summary>
    ''' all numbers click
    ''' </summary>
    Protected Sub number_click(sender As Object, e As System.EventArgs)
        'Get number from button text
        Dim Num = CInt(CType(sender, Button).Text)

        Dim items = EquationItems
        If items.Count > 0 Then
            Dim LastItem = items(items.Count - 1)
            ' Last item was number, so remove and add as digit!
            If TypeOf LastItem Is Integer Then
                '' prevent concat number more than integer
                If LastItem.ToString.Length < 9 Then
                    Num = CInt(LastItem.ToString & Num.ToString)
                Else
                    Num = CInt(LastItem)
                End If
                items.RemoveAt(items.Count - 1)

            End If
        End If

        items.Add(Num)
        EquationItems = items

        Update()
    End Sub

    ''' <summary>
    ''' all operand click
    ''' </summary>
    Protected Sub operand_click(sender As Object, e As System.EventArgs)
        'Get Operand from button text
        Dim Operand = CType(sender, Button).Text

        Dim items = EquationItems

        If items.Count > 0 Then
            Dim LastItem = items(items.Count - 1)
            ' Last item was operand, so remove it
            If TypeOf LastItem Is String Then
                items.RemoveAt(items.Count - 1)
            End If
        End If

        ' No items yet, can't add oeprand
        If items.Count > 0 Then items.Add(Operand)
        EquationItems = items

        Update()
    End Sub

    ''' <summary>
    ''' equal
    ''' </summary>
    Protected Sub equal_click(sender As Object, e As System.EventArgs)
        Update()
    End Sub

    ''' <summary>
    ''' back
    ''' </summary>
    Protected Sub back_click(sender As Object, e As System.EventArgs)
        If EquationItems.Count = 0 Then Exit Sub

        Dim items = EquationItems
        Dim LastItem = items(items.Count - 1)

        If TypeOf LastItem Is Integer Then
            ' Last item was number, so remove it
            items.RemoveAt(items.Count - 1)
        Else
            ' Last item was operand, so remove it and the one before
            items.RemoveRange(items.Count - 2, 2)
        End If

        EquationItems = items

        Update()
    End Sub

    Protected Sub clear_click(sender As Object, e As System.EventArgs)

        EquationItems = New List(Of Object)

        Update()
    End Sub
End Class
选项显式打开
选项严格限制在
部分类计算器
继承System.Web.UI.Page
''' 
''存储在viewstate中添加的项列表(数字和操作数)
''' 
私有属性EquationItems作为(对象的)列表
收到
如果ViewState(“EquationItems”)为Nothing,则返回(对象的)新列表
返回CType(ViewState(“EqualationItems”)、列表(对象))
结束
设置(值为列表(对象))
ViewState(“EqualationItems”)=值
端集
端属性
''' 
''参考方程和结果
''' 
私有子更新()
Dim等式=“”
将结果变暗为Double=0
对于i,整数=0到EquationItems.Count-1
尺寸项目=等式项目(i)
等式+=item.ToString&“
Dim Num As Integer=If(项目类型为整数,CInt(项目),-1)
如果i=0,那么
结果=如果(Num>-1,Num,0)
其他的
如果Num>-1,则
Dim LASTOPERNAM=EqualationItems(i-1)
结果=计算(结果,CStr(最后一个操作数),Num)
如果结束
如果结束
下一个
EquationLabel.Text=方程
ResultLabel.Text=Result.ToString
端接头
''' 
''为每个操作数计算
''' 
专用函数Calc(结果为Double,操作数为字符串,Num为整数)为Double
如果操作数=“+”,则
返回结果+Num
ElseIf Operand=“-”然后
返回结果-Num
ElseIf操作数=“*”然后
返回结果*Num
ElseIf Operand=“/”然后
返回If(Num 0,Result/Num,0)
如果结束
返回0
端函数
''' 
''所有数字点击
''' 
受保护的子编号\u单击(发件人作为对象,e作为System.EventArgs)
'从按钮文本获取编号
Dim Num=CInt(CType(发送者,按钮).Text)
尺寸项目=等式项目
如果items.Count>0,则
Dim LastItem=项目(items.Count-1)
'最后一项是数字,因此删除并添加为数字!
如果LastItem的类型为整数,则
“”防止concat数大于整数
如果LastItem.ToString.Length<9,则
Num=CInt(LastItem.ToString&Num.ToString)
其他的
Num=CInt(最后一项)
如果结束
items.RemoveAt(items.Count-1)
如果结束
如果结束
items.Add(Num)
等式项=项
更新()
端接头
''' 
''所有操作数单击
''' 
受保护的子操作数\u单击(发送方作为对象,e作为System.EventArgs)
'从按钮文本获取操作数
Dim操作数=CType(发送方,按钮)。文本
尺寸项目=等式项目
如果items.Count>0,则
Dim LastItem=项目(items.Count-1)
'最后一项是操作数,请将其删除
如果LastItem的类型为String,则
items.RemoveAt(items.Count-1)
如果结束
如果结束
'尚未添加项目,无法添加oeprand
如果items.Count>0,则items.Add(操作数)
等式项=项
更新()
端接头
''' 
“平等”
''' 
受保护的次相等单击(发送者作为对象,e作为System.EventArgs)
更新()
端接头
''' 
“回来
''' 
受保护的子备份\u单击(发件人作为对象,e作为System.EventArgs)
如果EquationItems.Count=0,则退出Sub
尺寸项目=等式项目
Dim LastItem=项目(items.Count-1)
如果LastItem的类型为整数,则
'最后一项是数字,请将其删除
items.RemoveAt(items.Count-1)
其他的
'最后一项是操作数,因此请删除它和之前的一项
items.RemoveRange(items.Count-2,2)
如果结束
等式项=项
更新()
端接头
受保护的子对象清除\u单击(发件人作为对象,e作为System.EventArgs)
EquationItems=新列表(对象)
更新()
端接头
末级

如果您不必使用“相等”按钮来获取结果,那么计算器如何知道您何时准备好获取结果?这一切都很好,但对我来说似乎有点进步。我仍在试图理解“为什么”这是可行的。你能解释一下下面这行代码吗?Private Property EquationItems作为(对象的)列表,仅存储列表中单击的数字或运算符,以便我可以对其执行操作。。该列表保存在viewstate中