变量不会改变

时间:2014-11-27 18:29:19

标签: vb.net variables visual-studio-2012

我正在尝试为课程完成一个项目,该项目使用InputBox对象计算酒店的入住率。但是,变量将永久设置为输入的第一个值。

请允许我澄清正在发生的事情。我点击按钮调出InputBox我输入一个数字,例如10,然后我点击确定,然后我输入第二个的新号码,例如7,但列表框显示为第一个。一旦我输入售卖房间的所有数字,然后继续输入可用房间的值,但是第一个循环的数字以某种方式转移到第二个循环并且不会改变。

我该怎么做才能使循环的每次迭代重置变量。我已经将变量添加到总数和列表中后将变量设置为0,但它不会发生变化。

我确实做过研究。 MSDN的知识库没有任何帮助,我只能在这里找到一个这样的问题,他只是忘了定义一个字符串,我试过谷歌但无济于事。

这是我的代码:

Public Class frmOccupancyRateCalculator

Private Sub frmOccupancyRateCalculator_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    lstRoomsSold.Items.Clear()
    lstRoomsAvailable.Items.Clear()

End Sub

Private Sub btnEnterRoomSales_Click(sender As Object, e As EventArgs) Handles btnEnterRoomSales.Click

    ' Declare the arithimetic and message variables.
    Dim intRoomsSold As Integer
    Dim intRoomsAvailable As Integer
    Dim intTotalRooms As Integer
    Dim intTotalRoomsSold As Integer
    Dim intTotalRoomsAvailable As Integer
    Dim intRoomsSoldNumberOfEntries As Integer = 1
    Dim intRoomsAvailableNumberOfEntries As Integer = 1
    Dim intNumberOfEntries As Integer = 1
    Dim intMaxNumberOfEntries As Integer = 7
    Dim decOccupancyRate As Decimal
    Dim strRoomsSold As String
    Dim strRoomsSoldInputMessage As String = "Enter the number of rooms sold for floor #"
    Dim strRoomsSoldInputHeading As String = "Enter Rooms Sold"
    Dim strRoomsSoldNormalMessage As String = "Enter the number of rooms sold for floor #"
    Dim strRoomsSoldNonNumericError As String = "Error - Non-Numeric value entered. Please enter a whole number of rooms sold for floor #"
    Dim strRoomsSoldNegativeError As String = "Error - Negative number entered. Please enter a whole number greater than zero of rooms sold for floor #"
    Dim strRoomsSoldDecimalError As String = "Error - Decimal number entered. Plerase enter a whole number of rooms sold for floor #"
    Dim strRoomsAvailable As String
    Dim strRoomsAvailableInputMessage As String = "Enter the number of rooms available for floor #"
    Dim strRoomsAvailableInputHeading As String = "Enter Rooms Available"
    Dim strRoomsAvailableNormalMessage As String = "Enter the number of rooms available for floor #"
    Dim strRoomsAvailableNonNumericError As String = "Error - Non-Numeric value entered. Please enter a whole number of rooms available for floor #"
    Dim strRoomsAvailableNegativeError As String = "Error - Negative number entered. Please enter a whole number greater than zero of rooms available for floor #"
    Dim strRoomsAvailableDecimalError As String = "Error - Decimal number entered. Plerase enter a whole number of rooms available for floor #"
    Dim strCancelClicked As String = ""

    ' Define the RoomsSoldInputMessage variables
    strRoomsSold = InputBox(strRoomsSoldInputMessage & intRoomsSoldNumberOfEntries, strRoomsSoldInputHeading, " ")

    ' Loop to iterate until hours of travel are entered for all days of travel
    Do Until intRoomsSoldNumberOfEntries > intMaxNumberOfEntries

        ' Is the input numeric?
        If IsNumeric(strRoomsSold) Then
            intRoomsSold = Convert.ToDecimal(strRoomsSold)

            ' Is the input greater or equal to 0?
            If intRoomsSold >= 0 Then

                ' Is the number of rooms sold a whole number?
                'If intRoomsSold Mod 1 = 0 Then

                intTotalRoomsSold += intRoomsSold
                lstRoomsSold.Items.Add(intRoomsSold)
                intRoomsSoldNumberOfEntries += 1
                intRoomsSold = 0
                strRoomsSoldInputMessage = strRoomsSoldNormalMessage

                ' Display decimal error message
                'Else
                'strRoomsSoldInputMessage = strRoomsSoldDecimalError
                'End If

                ' Display negative number error message
            Else
                strRoomsSoldInputMessage = strRoomsSoldNegativeError

            End If

            ' Display non-numeric error message
        Else
            strRoomsSoldInputMessage = strRoomsSoldNonNumericError

        End If

        ' Is the number of entries less than or equal to the maximum?
        If intRoomsSoldNumberOfEntries <= intMaxNumberOfEntries Then
            strRoomsSoldInputMessage = InputBox(strRoomsSoldInputMessage & intRoomsSoldNumberOfEntries, strRoomsSoldInputHeading, " ")

        End If

    Loop


    ' Define the RoomsAvailableInputMessage variable
    strRoomsAvailable = InputBox(strRoomsAvailableInputMessage & intRoomsAvailableNumberOfEntries, strRoomsAvailableInputHeading, " ")

    Do Until intRoomsAvailableNumberOfEntries > intMaxNumberOfEntries

        ' Is the input numeric?
        If IsNumeric(strRoomsAvailable) Then
            intRoomsAvailable = Convert.ToDecimal(strRoomsAvailable)

            ' Is the input greater or equal to 0?
            If intRoomsAvailable >= 0 Then

                ' Is the number of rooms sold a whole number?
                'If intRoomsAvailable Mod 1 = 0 Then

                intTotalRoomsAvailable += intRoomsAvailable
                lstRoomsAvailable.Items.Add(intRoomsAvailable)
                intRoomsAvailableNumberOfEntries += 1
                intRoomsAvailable = 0
                strRoomsAvailableInputMessage = strRoomsAvailableNormalMessage

                ' Is the number of entries equal to the maximum number of entries?
                If intRoomsAvailableNumberOfEntries = intMaxNumberOfEntries Then
                    intNumberOfEntries += 1
                End If

                ' Display decimal error message
                'Else
                ' strRoomsAvailableInputMessage = strRoomsAvailableDecimalError
                ' End If

                ' Display negative number error message
            Else
                strRoomsAvailableInputMessage = strRoomsAvailableNegativeError

            End If

            ' Display non-numeric error message
        Else
            strRoomsAvailableInputMessage = strRoomsAvailableNonNumericError

        End If

        ' Is the number of entries less than or equal to the maximum?
        If intRoomsAvailableNumberOfEntries <= intMaxNumberOfEntries Then
            strRoomsAvailableInputMessage = InputBox(strRoomsAvailableInputMessage & intRoomsAvailableNumberOfEntries, strRoomsAvailableInputHeading, " ")

        End If

    Loop

    ' Is the number of rooms sold entries greater than 1?
    If intNumberOfEntries > 1 Then

        ' Display result label and totals

        intTotalRooms = intTotalRoomsSold + intTotalRoomsAvailable
        decOccupancyRate = intTotalRoomsSold / intTotalRooms
        lblResult.Visible = True
        lblResult.Text = intTotalRooms & vbNewLine & intTotalRoomsSold & vbNewLine & intTotalRoomsAvailable & vbNewLine & vbNewLine & decOccupancyRate.ToString("P")

        ' Disable the Enter Room Sales button
        btnEnterRoomSales.Enabled = False

        ' Display error message for no values entered
    Else
        MsgBox("No Rooms Sold/Available value entered")

    End If

End Sub

Private Sub mnuClear_Click(sender As Object, e As EventArgs) Handles mnuClear.Click
    ' This script is executed when the user taps or clicks the Clear menu item.
    ' It clears the Room Sales and Rooms Available ListBoxes, hides the Result label, 
    ' enables the Enter Room Sales button.

    lstRoomsSold.Items.Clear()
    lstRoomsAvailable.Items.Clear()
    lblResult.Visible = False
    btnEnterRoomSales.Enabled = True

End Sub

Private Sub mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
    ' This script is executed when the user taps or clicks the Exit menu item.
    ' The window is closed and the program is terminated.

    Close()

End Sub
End Class

1 个答案:

答案 0 :(得分:2)

好的我明白了。我需要在循环中放入strRoomsSold和strRoomsAvailable变量定义,然后删除Do Until循环中的第二个序列。

        ' Loop to iterate until Rooms Sold number of Entries is greater than the maximum
    Do Until intRoomsSoldNumberOfEntries > intMaxNumberOfEntries

        ' Define the RoomsSold variable
        strRoomsSold = InputBox(strRoomsSoldInputMessage & intRoomsSoldNumberOfEntries, strRoomsSoldInputHeading, " ")

        If strRoomsSold = strCancelClicked Then
            strRoomsSoldInputMessage = strRoomsAvailableBlankError
        Else
            ' Is the input numeric?
            If IsNumeric(strRoomsSold) Then
                intRoomsSold = Convert.ToDecimal(strRoomsSold)

                ' Is the input greater or equal to 0?
                If intRoomsSold >= 0 Then

                    ' Is the number of rooms sold a whole number?
                    If intRoomsSold Mod 1 = 0 Then

                        intTotalRoomsSold += intRoomsSold
                        lstRoomsSold.Items.Add(intRoomsSold)
                        intRoomsSoldNumberOfEntries += 1

                        strRoomsSoldInputMessage = strRoomsSoldNormalMessage

                        ' Display decimal error message
                    Else
                        strRoomsSoldInputMessage = strRoomsSoldDecimalError
                    End If

                    ' Display negative number error message
                Else
                    strRoomsSoldInputMessage = strRoomsSoldNegativeError

                End If

                    ' Display non-numeric error message
            Else
                strRoomsSoldInputMessage = strRoomsSoldNonNumericError

            End If

        End If
    Loop



    Do Until intRoomsAvailableNumberOfEntries > intMaxNumberOfEntries

        ' Define the RoomsAvailable variable
        strRoomsAvailable = InputBox(strRoomsAvailableInputMessage & intRoomsAvailableNumberOfEntries, strRoomsAvailableInputHeading, " ")

        ' Is the input numeric?
        If IsNumeric(strRoomsAvailable) Then
            intRoomsAvailable = Convert.ToDecimal(strRoomsAvailable)

            ' Is the input greater or equal to 0?
            If intRoomsAvailable >= 0 Then

                ' Is the number of rooms sold a whole number?
                If intRoomsAvailable Mod 1 = 0 Then

                    intTotalRoomsAvailable += intRoomsAvailable
                    lstRoomsAvailable.Items.Add(intRoomsAvailable)
                    intRoomsAvailableNumberOfEntries += 1

                    strRoomsAvailableInputMessage = strRoomsAvailableNormalMessage

                    ' Is the number of entries equal to the maximum number of entries?
                    If intRoomsAvailableNumberOfEntries = intMaxNumberOfEntries Then
                        intNumberOfEntries += 1
                    End If

                    ' Display decimal error message
                Else
                    strRoomsAvailableInputMessage = strRoomsAvailableDecimalError
                End If

                ' Display negative number error message
            Else
                strRoomsAvailableInputMessage = strRoomsAvailableNegativeError

            End If

                ' Display non-numeric error message
            Else
                strRoomsAvailableInputMessage = strRoomsAvailableNonNumericError

            End If

    Loop