有些人可以告诉我我做错了什么。 我是visual basic 2012的新手
我想做什么:
一个输入保存到Integer变量中。另一个输入保存到Byte变量中。 输出计算为Integer * Byte / 100到功率2。 如果用户输入非数字值并单击该按钮,则显示错误"输入非法值"使用Messagebox。 如果用户输入"超出范围"数字,显示错误"输入不在有效范围内"使用Messagebox。
这是我的代码:
Public Class Form1
Dim T1 As String
Dim T2 As String
Dim a As Integer
Dim b As Byte
Dim c As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
TextBox1.Text = False
TextBox2.Text = False
a = CInt(TextBox1.Text)
b = CByte(TextBox2.Text)
c = a * b / 100
c = c * a * b / 100 ^ 2
Label3.Text = "Output:" + c.ToString()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Illegal Value Entered")
Catch ex As IndexOutOfRangeException
MessageBox.Show("Input not in valid range")
Catch ex As Exception
MessageBox.Show("Some exception has occured")
End Try
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:0)
请删除表达式TextBox1.Text = False(对于TextBox2)这将始终强制CInt()和CByte()解析字符串'False'(不是用户提供的数字)。您的代码应如下所示。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
a = CInt(TextBox1.Text)
b = CByte(TextBox2.Text)
c = a * b / 100
c = c * a * b / 100 ^ 2
Label3.Text = "Output:" + c.ToString()
Catch ex As ArgumentOutOfRangeException
MessageBox.Show("Illegal Value Entered")
Catch ex As IndexOutOfRangeException
MessageBox.Show("Input not in valid range")
Catch ex As Exception
MessageBox.Show("Some exception has occured")
End Try
End Sub
如果您想在计算后清除文本框中的数字,请将这些代码行放在Label3.Text = ... line
之后TextBox1.Text = False
TextBox2.Text = False
CByte文档明确指出
如果表达式超出字节子类型的可接受范围, 发生错误。
CByte(...)构造函数无法解析任何大于255的数字。