首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB InputBox验证查询

VB InputBox验证查询
EN

Stack Overflow用户
提问于 2017-03-29 10:04:28
回答 1查看 60关注 0票数 0

我知道InputBox不是最好的验证工具,但这是我为class编写的程序的规范之一。我的问题是,尽管我创建了if或case语句来验证输入的数据,但它仍然接受数据,同时显示我的代码中的MsgBox。

基本上,我希望case语句所做的是正确地过滤输入的数据,如果数据无效,则不会进入下一层,并请求输入新的数据。如果数据有效,则进入下一层。

代码语言:javascript
复制
    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
EN

回答 1

Stack Overflow用户

发布于 2017-03-30 13:01:59

我花了一点时间检查您的代码,实际上,您需要一个布尔值来验证是否满足占用要求,如果不满足,则执行循环。代码应该是这样的:

代码语言:javascript
复制
    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,它将为您照亮网站的正常使用方式

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43083006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档