VB.NET ----计算器

最近接触了一下VB.NET,并根据网上的资料写了一个计算器,代码如下:

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        输入(1)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        输入(2)
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        输入(3)
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        输入(4)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        输入(5)
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        输入(6)
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        输入(7)
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        输入(8)
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        输入(9)
    End Sub
    Private Sub 输入(数值 As String)
        If 屏幕.Text = "0" Then
            屏幕.Text = 数值
        Else
            屏幕.Text &= 数值
        End If
    End Sub

    Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.Click
        If 屏幕.Text = "0" Then

        Else
            屏幕.Text &= 0
        End If
    End Sub

    Private Sub 点_Click(sender As Object, e As EventArgs) Handles 点.Click
        屏幕.Text &= "."
    End Sub
    Dim 上一次的值 As Double = 0
    Dim 运算符 As String = "+"
    Private Sub 加号_Click(sender As Object, e As EventArgs) Handles 加号.Click
        If Not IsNumeric(屏幕.Text) Then Return
        上一次的值 = 屏幕.Text
        屏幕.Text = 0
        运算符 = "+"

    End Sub

    Private Sub 等于号_Click(sender As Object, e As EventArgs) Handles 等于号.Click
        If Not IsNumeric(屏幕.Text) Then
            MsgBox("输入的内容无法计算!")
            Return
        End If
        Select Case 运算符
            Case "+"
                屏幕.Text = 上一次的值 + CDbl(屏幕.Text)
            Case "-"
                屏幕.Text = 上一次的值 - CDbl(屏幕.Text)
            Case "*"
                屏幕.Text = 上一次的值 * CDbl(屏幕.Text)
            Case "/"
                屏幕.Text = 上一次的值 / CDbl(屏幕.Text)
        End Select
        上一次的值 = 屏幕.Text
    End Sub

    Private Sub CE_Click(sender As Object, e As EventArgs) Handles CE.Click
        屏幕.Text = 0
        上一次的值 = 0
        运算符 = "+"
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles 除号.Click
        If Not IsNumeric(屏幕.Text) Then Return
        上一次的值 = 屏幕.Text
        屏幕.Text = 0
        运算符 = "/"
    End Sub

    Private Sub 乘_Click(sender As Object, e As EventArgs) Handles 乘.Click
        If Not IsNumeric(屏幕.Text) Then Return
        上一次的值 = 屏幕.Text
        屏幕.Text = 0
        运算符 = "*"
    End Sub

    Private Sub 减号_Click(sender As Object, e As EventArgs) Handles 减号.Click
        If Not IsNumeric(屏幕.Text) Then Return
        If 屏幕.Text = "0" Then
            屏幕.Text = "-"
        Else 上一次的值 = 屏幕.Text
            屏幕.Text = 0
            运算符 = "-"
        End If
    End Sub
End Class

你可能感兴趣的:(VB.NET)