VB InputBox验证查询

时间:2017-03-29 02:04:28

标签: vb.net inputbox

我知道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

1 个答案:

答案 0 :(得分:0)

有一点时间检查你的代码,实际上你需要一个布尔值来验证是否满足占用要求,如果没有则循环。代码将是这样的:

    For floor = 1 To MAX_FLOOR
        'Boolean to validate the occupancy meet the requirements
        Dim goodOccupancy As Boolean = False
        'Do loop enters at least 1 time and runs the code inside it
        Do

            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)
                    'If the requirements are met we change the Boolean value to continue with the execution
                    goodOccupancy = True
            End Select
            'We loop if the requirements are not met
        Loop Until goodOccupancy = True
        output.Items.Add("Floor: " & floor & " Rooms Occupied: " & occupancy _
                        & " Occupancy Rate: " & occupancyRate.ToString("P2"))
        lblRoomsOccupied.Text = occupancySum.ToString
        lblOccupancyRate.Text = overallRate.ToString("P2")

    Next

但请下次尝试更明确地使用您的代码。大多数人不会检查发生了什么。不要指望人们在没有指导的情况下解决您的问题。请阅读How to ask guide它将为您提供使用该网站的灯光,因为它应该被使用