【发布时间】:2017-03-29 02:04:27
【问题描述】:
我知道 InputBox 不是验证的最佳选择,但这是我为课堂编写的程序的规范之一。我的问题是,尽管我使用 if 或 case 语句来验证输入的数据,但它仍然接受数据,同时显示我在代码中拥有的 MsgBox。
基本上我希望case语句做的是正确过滤输入的数据,如果数据无效则不继续下一层并请求输入新数据。如果数据有效,则进入下一层。
Const ROOMS As Integer = 30
Const MAX_FLOOR As Integer = 16
Dim floor As Integer
Dim StrOccupancy As String
Dim occupancy As Integer
Dim occupancyRate As Double
Dim occupancySum As Integer
Dim overallRate As Double
lblOccupancyRate.Text = String.Empty
lblRoomsOccupied.Text = String.Empty
output.Items.Clear()
For floor = 1 To MAX_FLOOR
If floor = 13 Then
Continue For
End If
StrOccupancy = Integer.TryParse(InputBox("Enter the number of rooms occupied for floor:" & Space(1) & floor), occupancy)
Select Case occupancy
Case < 1
MsgBox("Please enter a number of 1 or more occupants.")
Case > 30
MsgBox("Amount of occupants must be between 1-30.")
Case >= 1 And occupancy <= 30
occupancyRate = (occupancy / ROOMS)
occupancySum += occupancy
overallRate = occupancySum / (ROOMS * 15)
End Select
output.Items.Add("Floor: " & floor & " Rooms Occupied: " & occupancy _
& " Occupancy Rate: " & occupancyRate.ToString("P2"))
lblRoomsOccupied.Text = occupancySum.ToString
lblOccupancyRate.Text = overallRate.ToString("P2")
Next
output.Items.Add("")
output.Items.Add("Total occupancy is" & Space(1) & occupancySum & Space(1) & "and" & Space(1) & overallRate.ToString("P2") & Space(1) & " of rooms are full.")
End Sub
【问题讨论】:
-
“我应该添加或更改什么才能使其正常运行?” ——嗯,你还没有定义“正常运行”是什么意思。因此,如果由我决定,我会加一杯朗姆酒和几个墨西哥卷饼。
-
您遇到的问题是您的`Select Case` 使用了错误的变量。它应该是
StrOccupancy而不是occupancy -
好吧,我希望它能够正确过滤输入的数据,如果根据我的案例陈述数据无效,我希望它不会继续到下一层。虽然,酒精和一些不错的法加它听起来不错现在真的很好..
-
@3vts 不是问题。我使用 TryParse 方法将数据转换为具有变量占用的整数。任何一个仍然让我遇到同样的问题。
-
@Gonzo:“不进入下一层”会是什么样子?只是停止执行?要求用户输入当前楼层的新数据?将 Ken 的墨西哥卷饼送货员重定向到我家?