在 Do While 循环中只接受来自 InputBox VB.Net 的整数值
Only accept Integer values from InputBox VB.Net within Do While Loop
我想弄清楚为什么我的程序接受非整数值,然后通过舍入将其转换为整数。用户输入的第一个值经过验证,但其余值未经过验证。我的程序旨在计算用户输入的奇数和偶数的数量。任何帮助将不胜感激。
Private Sub btnShow_Click(sender As System.Object, e As System.EventArgs) Handles btnAddNum.Click
Dim intInputNum As Integer
Dim strInputNum As String = ""
Dim isEven As Integer = 0
Dim isOdd As Integer = 0
Dim userInput As String = InputBox(("Enter a number (0 to end)"))
Dim numArray() As String = strInputNum.Split(New Char() {","c})
Dim validInput As Boolean = Integer.TryParse(userInput, intInputNum)
If validInput = False Then
MsgBox("Invalid input, integer numbers only")
ElseIf validInput = True Then
Do While intInputNum > 0 And validInput = True
If intInputNum Mod 2 = 0 And validInput = True Then
lblEvenValues.Text = lblEvenValues.Text & CStr(intInputNum) & ","
intInputNum = Val(InputBox("Enter a number (0 to end)"))
isEven = isEven + 1
lblCountEven.Text = isEven
ElseIf intInputNum Mod 2 <> 0 And validInput = True Then
lblOddValues.Text = lblOddValues.Text & CStr(intInputNum) & ","
intInputNum = Val(InputBox("Enter a number (0 to end)"))
isOdd = isOdd + 1
lblCountOdd.Text = isOdd
ElseIf validInput = False Then
MsgBox("Invalid input, integer numbers only")
End If
Loop
End If
End Sub
这一行:
Do While intInputNum > 0
给您带来了麻烦。当用户输入非整数值时,在第一次循环后,此行:Val(InputBox("Enter a number (0 to end)"))
的计算结果为零。
显然不大于0.
重新使用您的布尔语句来评估 userInput,而不是像这样的 intInputNum:
userInput = Val(InputBox("Enter a number (0 to end)"))
validInput = Integer.TryParse(userInput, intInputNum)
然后您需要修复消息框的位置,使其仅显示在循环之外而不是循环中。所以当 validInput <> True
它会打破循环并输出你的 Msgbox
.
您还需要修复 Do While
语句以正确评估 userInput 而不是 intInputNum
我想弄清楚为什么我的程序接受非整数值,然后通过舍入将其转换为整数。用户输入的第一个值经过验证,但其余值未经过验证。我的程序旨在计算用户输入的奇数和偶数的数量。任何帮助将不胜感激。
Private Sub btnShow_Click(sender As System.Object, e As System.EventArgs) Handles btnAddNum.Click
Dim intInputNum As Integer
Dim strInputNum As String = ""
Dim isEven As Integer = 0
Dim isOdd As Integer = 0
Dim userInput As String = InputBox(("Enter a number (0 to end)"))
Dim numArray() As String = strInputNum.Split(New Char() {","c})
Dim validInput As Boolean = Integer.TryParse(userInput, intInputNum)
If validInput = False Then
MsgBox("Invalid input, integer numbers only")
ElseIf validInput = True Then
Do While intInputNum > 0 And validInput = True
If intInputNum Mod 2 = 0 And validInput = True Then
lblEvenValues.Text = lblEvenValues.Text & CStr(intInputNum) & ","
intInputNum = Val(InputBox("Enter a number (0 to end)"))
isEven = isEven + 1
lblCountEven.Text = isEven
ElseIf intInputNum Mod 2 <> 0 And validInput = True Then
lblOddValues.Text = lblOddValues.Text & CStr(intInputNum) & ","
intInputNum = Val(InputBox("Enter a number (0 to end)"))
isOdd = isOdd + 1
lblCountOdd.Text = isOdd
ElseIf validInput = False Then
MsgBox("Invalid input, integer numbers only")
End If
Loop
End If
End Sub
这一行:
Do While intInputNum > 0
给您带来了麻烦。当用户输入非整数值时,在第一次循环后,此行:Val(InputBox("Enter a number (0 to end)"))
的计算结果为零。
显然不大于0.
重新使用您的布尔语句来评估 userInput,而不是像这样的 intInputNum:
userInput = Val(InputBox("Enter a number (0 to end)"))
validInput = Integer.TryParse(userInput, intInputNum)
然后您需要修复消息框的位置,使其仅显示在循环之外而不是循环中。所以当 validInput <> True
它会打破循环并输出你的 Msgbox
.
您还需要修复 Do While
语句以正确评估 userInput 而不是 intInputNum