所以我正在为visual studio 2012制作一个计算器。除了一件小事之外,它就像完成了95%。当用户试图除以0时,我需要显示一条错误消息。我尝试在线搜索,但无法找到确定的方法。你究竟是怎么做到的?我的代码如下 选项明确的 公共班级计算器
Dim FirstNumber As Single
Dim SecondNumber As Single
Dim AnswerNumber As Single
Dim ArithmeticProcess As String
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
txtAnswer.Text = txtAnswer.Text & 1
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
txtAnswer.Text = txtAnswer.Text & 2
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
txtAnswer.Text = txtAnswer.Text & 3
End Sub
Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
txtAnswer.Text = txtAnswer.Text & 4
End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
txtAnswer.Text = txtAnswer.Text & 5
End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
txtAnswer.Text = txtAnswer.Text & 6
End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
txtAnswer.Text = txtAnswer.Text & 7
End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
txtAnswer.Text = txtAnswer.Text & 8
End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
txtAnswer.Text = txtAnswer.Text & 9
End Sub
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
txtAnswer.Text = txtAnswer.Text & 0
End Sub
Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
txtAnswer.Text = txtAnswer.Text & "."
End Sub
Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
FirstNumber = Val(txtAnswer.Text)
txtAnswer.Text = "0"
ArithmeticProcess = "+"
End Sub
Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
FirstNumber = Val(txtAnswer.Text)
txtAnswer.Text = "0"
ArithmeticProcess = "-"
End Sub
Private Sub BtnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
FirstNumber = Val(txtAnswer.Text)
txtAnswer.Text = "0"
ArithmeticProcess = "X"
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
FirstNumber = Val(txtAnswer.Text)
txtAnswer.Text = "0"
ArithmeticProcess = "/"
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
SecondNumber = Val(txtAnswer.Text)
If ArithmeticProcess = "+" Then
AnswerNumber = FirstNumber + SecondNumber
End If
If ArithmeticProcess = "-" Then
AnswerNumber = FirstNumber - SecondNumber
End If
If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If
If ArithmeticProcess = "/" Then
AnswerNumber = FirstNumber / SecondNumber
End If
txtAnswer.Text = AnswerNumber
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
Me.txtAnswer.Clear()
End Sub
结束班
答案 0 :(得分:2)
使用Try ... Catch块在计算时捕获异常。
Try
answerNumber = firstNumber / secondNumber
Catch ex As OverflowException
MsgBox("cannot divide by 0")
End Try
答案 1 :(得分:1)
您不需要捕获错误,您需要验证输入....
C#
if (SecondNumber == 0)
{
//you are trying to divide by 0
}
VB.NET
If SecondNumber = 0 Then
//you are trying to divide by 0
End If
答案 2 :(得分:0)
将Try功能作为一个捕捉所有代码和一个“零捕手”
Try
''''your code
Catch ex As OverflowException
MsgBox("Sorry Something went wrong")
End Try
并停止' 0'开始跑...
If ArithmeticProcess = "/" Then
If SecondNumber <> 0 Then
MsgBox("You cannot divide by Zero")
Else
AnswerNumber = FirstNumber / SecondNumber
End If
End If