棒球门票 - VB - 没有计算正确的价格?

时间:2011-11-09 07:36:04

标签: vb.net visual-studio-2010

Public Class Form1
Dim intTicketChoice As Integer
Dim finalCost As Decimal
Dim cost As Decimal
Dim seatType As Integer


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Threading.Thread.Sleep(5000)
End Sub

Private Sub cboTypeOfTicket_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboTypeOfTicket.SelectedIndexChanged

    intTicketChoice = Me.cboTypeOfTicket.SelectedIndex
    Me.ListBox1.Items.Clear()
    EnableObjects()

    Select Case intTicketChoice
        Case 0
            SingleGame(seatType)
        Case 1
            Season(seatType)
    End Select
End Sub

Sub EnableObjects()
    lblNumTickets.Visible = True
    txtNumOfTickets.Visible = True
    lblSeatType.Visible = True
    ListBox1.Visible = True
    lblAnswerText.Visible = False
    lblCost.Visible = False
    btnClear.Visible = True
    btnCompute.Visible = True
End Sub

Private Sub SingleGame(ByVal seatType As Integer)

    Me.ListBox1.Items.Add("Box Seats $55")
    Me.ListBox1.Items.Add("Lower Deck Seats $35")
    Me.ListBox1.Items.Add("Upper Deck Seats $25")
    Me.ListBox1.Items.Add("Standing Room Only $15")


    If ListBox1.SelectedItem = "Box Seats $55" Then
        seatType = 0
    ElseIf ListBox1.SelectedItem = "Lower Deck Seats $35" Then
        seatType = 1
    ElseIf ListBox1.SelectedItem = "Upper Deck Seats $25" Then
        seatType = 2
    ElseIf ListBox1.SelectedItem = "Standing Room Only $15" Then
        seatType = 3
    End If

End Sub

Private Sub Season(ByVal seatType As Integer)

    Me.ListBox1.Items.Add("Box Seats $2500")
    Me.ListBox1.Items.Add("Lower Deck Seats $1500")


    If ListBox1.SelectedItem = "Box Seats $2500" Then
        seatType = 4
    Elseif ListBox1.SelectedItem = "Lower Deck Seats $1500" Then
        seatType = 5
    End If

End Sub

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click
    Dim ticketNum As Integer

    ticketNum = Convert.ToInt32(txtNumOfTickets.Text)
    intTicketChoice = cboTypeOfTicket.SelectedIndex

    Dim totalCost As Decimal = GetGameCost(ticketNum)


    Try
        ticketNum = Convert.ToInt32(txtNumOfTickets.Text)
    Catch Exception As FormatException
        MsgBox("Number of tickets must be numeric")
        Return

    End Try


    'Display cost:
    lblAnswerText.Visible = True
    Me.lblCost.Text = totalCost.ToString("C")
    lblCost.Visible = True


End Sub

Private Function GetGameCost(ByVal ticketNum As Integer)

    If seatType = 0 Then
        finalCost = (ticketNum * 55D)
    ElseIf seatType = 1 Then
        finalCost = (ticketNum * 35D)
    ElseIf seatType = 2 Then
        finalCost = (ticketNum * 25D)
    ElseIf seatType = 3 Then
        finalCost = (ticketNum * 15D)
    ElseIf seatType = 4 Then
        finalCost = (ticketNum * 2500D)
    ElseIf seatType = 5 Then
        finalCost = (ticketNum * 1500D)
    End If
    Return finalCost
End Function

结束班

嗨,伙计们,我被困在我的VB作业上。嘿,我是新来的,所以不要把我撕成碎片,呵呵。无论如何,每当我尝试计算时,它似乎给我55 * ticketnum而不是寻找实际的seatType并使用那个。我在这里看到了关于同样问题的另一个问题,但找不到我确切问题的答案。

任何想法或提示?

1 个答案:

答案 0 :(得分:0)

我们不会把你撕成碎片,实际上很高兴得到一个家庭作业问题,其中海报实际上已经完成某事而不是在这里逐字地转移作业问题,期待成为勺子-fed: - )

在这种情况下,我会看到您通过值将seatType传递给SingleGameSeason这一事实。

这意味着只会对本地副本进行任何更改,而不是对象变量。对象变量将保持其初始值零,这样,当您计算价格时,它总是乘以55.

通过引用传递以便将更改反映回对象变量,或者仅使用对象变量本身(通过不将其作为参数传递)。