我应该使用循环而不是 if elseif 吗?

问题描述 投票:0回答:0

公开课表1

'declare class level variables
Dim intRoom As Integer
Dim intTotalRooms As Integer
Dim dblOccupancyRate As Double
Dim dblOverallOccupancyRate As Double

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim intFloor As Integer

    'select the floor level and change to the next floor
    If cboFloor.SelectedItem = 1 Then
        intFloor = 1
        cboFloor.SelectedIndex = 1   'changes the combo box to the next floor
    ElseIf cboFloor.SelectedItem = 2 Then
        intFloor = 2
        cboFloor.SelectedIndex = 2
    ElseIf cboFloor.SelectedItem = 3 Then
        intFloor = 3
        cboFloor.SelectedIndex = 3
    ElseIf cboFloor.SelectedItem = 4 Then
        intFloor = 4
        cboFloor.SelectedIndex = 4
    ElseIf cboFloor.SelectedItem = 5 Then
        intFloor = 5
        cboFloor.SelectedIndex = 5
    ElseIf cboFloor.SelectedItem = 6 Then
        intFloor = 6
        cboFloor.SelectedIndex = 6
    ElseIf cboFloor.SelectedItem = 7 Then
        intFloor = 7
        cboFloor.SelectedIndex = 7
    ElseIf cboFloor.SelectedItem = 8 Then
        intFloor = 8
    End If

    'get room total from textbox and assign to introom variable
    If Integer.TryParse(txtRooms.Text, intRoom) Then
        If intRoom > 0 And intRoom <= 30 Then  'test introom is between 1 and 30

            dblOccupancyRate = intRoom / 30   'occupancty rate=rooms/total rooms
            intTotalRooms += intRoom          'intTotalRooms is accumulator

            dblOverallOccupancyRate = intTotalRooms / 240  'occupancy of whole hotel
        End If
    Else
        MessageBox.Show("Please enter a positive number")   'error message for txtRooms
    End If

    'creating output for listbox 
    lstOutput.Items.Add("Floor:" & intFloor & "   Rooms occupied:" & intRoom &
                            "   Occupancy rate:" & dblOccupancyRate.ToString("P2"))

    'clearing the textbox after each time you hit save
    txtRooms.Clear()

    'display total rooms occupied
    lblRoomsOccupied.Text = intTotalRooms.ToString

    'display overall occupancy rate
    lblOverall.Text = dblOverallOccupancyRate.ToString("p2")

Hotel occupancy form

我的代码产生了正确的计算,我只是想知道是否有人以不同的方式完成了这个程序?我觉得地板组合框应该有一个循环,但我不知道每次循环迭代时如何在文本框中输入占用的房间。我在想你可以在每次循环迭代时提示一个输入框,但教科书并不要求输入框。如果需要,我可以提供教科书问题的副本,我尽量不让信息过多。

loops basic
© www.soinside.com 2019 - 2024. All rights reserved.