【问题标题】:VB InputBox Validation InquiryVB InputBox验证查询
【发布时间】: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 的墨西哥卷饼送货员重定向到我家?

标签: vb.net inputbox


【解决方案1】:

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

    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,它会让您轻松使用该网站,因为它应该被使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2011-06-26
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    相关资源
    最近更新 更多