本文介绍了Vb计算器问题,多次点击相等按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我单击一个数字,添加它(变为var1),再次单击数字,按下相等(第二个数字变为var2)并进行操作。现在当我再次点击相等(这使得现在输出var2)时,它将它添加到var1。我想要做的是如果你再次点击相等,var1将改为第一个var 2.我不是很擅长解释XD但是这里 1 + 3 = 4 1(按下一个操作)使1变为var1 然后3(按相等)使3变为var2并运行它 现在如果我再次按下相同,标签1上的是什么,我的var2是4 我的var1仍然是1但是我的var2是4所以它是这样的: 再按一次1 + 4 = 5而不是4 + 3 = 7使4变为1并保持3变量2 我尝试过: Public Class Form1 Dim var1 As Decimal Dim var2 As Decimal 作为字符串的暗淡操作 私有子按钮16_Click(发件人作为对象,e作为EventArgs)处理Button16.Click 如果Val(Label1。文字)<> 0然后 Label1.Text& = 0 Else Label1.Text = 0 结束如果 结束Sub Private Sub Button1_Click(发送者作为对象,e作为EventArgs)处理Button1.Click 如果Label1.Text<> 0然后 Label1.Text& = 1 Else Label1.Text = 1 结束如果 结束Sub Private Sub Button2_Click(发送者为对象,e为EventArgs)处理Button2.Click 如果Label1.Text<> ; 0然后 Label1.Text& = 2 Else Label1.Text = 2 结束如果 结束子 私有子按钮3_Click(发件人为对象,e为EventArgs)处理Button3.Click 如果Label1.Text<> ; 0然后 Label1.Text& = 3 Else Label1.Text = 3 结束如果 结束子 私有子按钮4_Click(发件人为对象,e为EventArgs)处理Button4。点击 如果Label1.Text<> ; 0然后 Label1.Text& = 4 Else Label1.Text = 4 结束如果 结束子 私人子按钮5_Click(发件人为对象,e为EventArgs)处理Button5。点击 如果Label1.Text<> ; 0然后 Label1.Text& = 5 Else Label1.Text = 5 结束如果 结束子 私人子按钮6_Click(发件人作为对象,e作为EventArgs)处理Button6.Click 如果Label1.Text<> ; 0然后 Label1.Text& = 6 Else Label1.Text = 6 结束如果 结束子 私有子按钮7_Click(发件人为对象,e为EventArgs)处理Button7.Click 如果Label1.Text<> ; 0然后 Label1.Text& = 7 Else Label1.Text = 7 结束如果 结束子 私人子按钮8_Click(发件人为对象,e为EventArgs)处理Button8.Click 如果Label1.Text<> ; 0然后 Label1.Text& = 8 Else Label1.Text = 8 结束如果 结束子 私有子按钮9_Click(发件人为对象,e为EventArgs)处理Button9.Click 如果Label1.Text<> ; 0然后 Label1.Text& = 9 Else Label1.Text = 9 结束如果 结束子 私有子按钮11_Click(发送者作为对象,e作为EventArgs)处理Button11.Click If IsNumeric(Label1.Text) <> 0然后 var1 = Label1.Text Label1.Text = operation =+ 结束如果 结束子 私人子按钮12_Click(发件人作为对象,e作为EventArgs)处理Button12.Click If IsNumeric(Label1.Text)<> 0然后 var1 = Label1.Text Label1.Text = operation = - 结束如果 结束子 私人子按钮13_Click(发件人作为对象,e作为EventArgs)处理Button13.Click If IsNumeric(Label1.Text)那么 var1 = Label1.Text Label1.Text = operation =* 结束如果 结束子 私人子按钮14_Click(发件人作为对象,e作为EventArgs)处理Button14 .Click 如果是IsNumeric(Label1.Text)那么 var1 = Label1.Text Label1.Text = operation =/ 结束如果 结束次级 私人子按钮10_点击(发件人为对象,e作为EventArgs)处理Button10.Click If IsNumeric(Label1.Text)Then var2 = Label1.Text 选择案例操作 案例+ Label1.Text = var1 + var2 案例 - Label1.Text = var1 - var2 案例* Label1.Text = var1 * var2 案例/ 如果var2 = 0那么 Label1.Text =错误! Else Label1.Text = var1 / var2 结束如果 结束选择 结束如果 结束子 私人子按钮15_Click(发件人为对象,e为EventArgs)处理Button15.Click Label1.Text =0 operation = var1 = 0 var2 = 0 结束次级 Private Sub Button17_Click(发送者作为对象,e作为EventArgs)处理Button17.Click Label1.Text& =。 结束Sub 结束类I click a number, added it(becomes var1), click number again, press equal(2nd number becomes var2) and operates it. now when i click equal again(which is making the now output var2) it adds it to var1. what i want to do is if you click equal again the var1 will change to the first var 2. Im not really good at explaining XD but here1 + 3 = 41(press an operation) making 1 the var1then 3(press equal) making 3 the var2 and operates itnow if i press equal again, whats on the label1 will be my var2 which is 4my var1 still 1 but my var2 is 4 so itd be like this:press equal again 1+4=5 and not 4+3=7 making 4 the var 1 and keeping 3 the var2What I have tried:Public Class Form1 Dim var1 As Decimal Dim var2 As Decimal Dim operation As String Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click If Val(Label1.Text) <> 0 Then Label1.Text &= 0 Else Label1.Text = 0 End If End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Label1.Text <> "0" Then Label1.Text &= 1 Else Label1.Text = 1 End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click If Label1.Text <> "0" Then Label1.Text &= 2 Else Label1.Text = 2 End If End Sub Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click If Label1.Text <> "0" Then Label1.Text &= 3 Else Label1.Text = 3 End If End Sub Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click If Label1.Text <> "0" Then Label1.Text &= 4 Else Label1.Text = 4 End If End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click If Label1.Text <> "0" Then Label1.Text &= 5 Else Label1.Text = 5 End If End Sub Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click If Label1.Text <> "0" Then Label1.Text &= 6 Else Label1.Text = 6 End If End Sub Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click If Label1.Text <> "0" Then Label1.Text &= 7 Else Label1.Text = 7 End If End Sub Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click If Label1.Text <> "0" Then Label1.Text &= 8 Else Label1.Text = 8 End If End Sub Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click If Label1.Text <> "0" Then Label1.Text &= 9 Else Label1.Text = 9 End If End Sub Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click If IsNumeric(Label1.Text) <> 0 Then var1 = Label1.Text Label1.Text = "" operation = "+" End If End Sub Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click If IsNumeric(Label1.Text) <> 0 Then var1 = Label1.Text Label1.Text = "" operation = "-" End If End Sub Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click If IsNumeric(Label1.Text) Then var1 = Label1.Text Label1.Text = "" operation = "*" End If End Sub Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click If IsNumeric(Label1.Text) Then var1 = Label1.Text Label1.Text = "" operation = "/" End If End Sub Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click If IsNumeric(Label1.Text) Then var2 = Label1.Text Select Case operation Case "+" Label1.Text = var1 + var2 Case "-" Label1.Text = var1 - var2 Case "*" Label1.Text = var1 * var2 Case "/" If var2 = 0 Then Label1.Text = "Error!" Else Label1.Text = var1 / var2 End If End Select End If End Sub Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click Label1.Text = "0" operation = "" var1 = 0 var2 = 0 End Sub Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click Label1.Text &= "." End SubEnd Class推荐答案您可以将所有这些按钮点击处理程序替换为一个,如下所示:You can replace all those button click handlers by one, like this:Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click Dim index As Integer If Integer.TryParse(CType(sender, Button).Tag.ToString, index) Then ' a number ' etc ... 这篇关于Vb计算器问题,多次点击相等按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 12:04