Visual Basic电影程序

时间:2015-04-16 01:33:38

标签: vb.net

我正在为这个我正在上课的VB程序遇到一个奇怪的问题。我老实说不完全确定我为什么遇到这个问题,因为我已经轻松完成了其他与此类似的任务。该程序旨在允许我从商店(左侧列表框)添加某些电影到购物车(右侧列表框)。您可以看到我添加了两次蜘蛛侠(因为谁不喜欢蜘蛛侠) ?)它确实显示了名称和每次2美元。然而,每次我将电影添加到框中时,底部的第一个标签框应该累积,但它在我选择的第一部电影时保持稳定。如果我要选择任何其他更昂贵的电影,它仍然会留在我选择的第一部电影中。

编辑:我应该补充一下,在将代码的某些部分移动到各自的功能之前,我遇到了同样的问题。

enter image description here

Public Class mainForm

Public strMovies() As String =
        {"Spider-Man", "Daredevil", "Hulk", "The Punisher", "Spider-Man 2",
         "Fantastic Four", "Spider-Man 3", "Iron Man", "The Amazing Spider-Man", "The Wolverine"}
Public intMoviePrices() As Integer =
    {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Dim X As Integer
Dim Y As Integer
Dim moviecost As Integer
Dim movietax As Double
Dim numberdvds As Integer
Dim shippingcharge As Double
Dim netcost As Double
Dim movieChoice As String


Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCom.SelectedIndexChanged

End Sub


Public Function calculateTotals(movieChoice) As Integer



    moviecost = moviecost + intMoviePrices(movieChoice)
    movietax = (moviecost * 1.04) - moviecost
    If numberdvds >= 5 Then
        shippingcharge = 5
    Else
        shippingcharge = numberdvds
    End If
    netcost = movietax + moviecost + shippingcharge

    lblgrosscost.Text = moviecost
    lblsalestax.Text = FormatNumber(movietax, 2)
    lblshipping.Text = shippingcharge
    lblnetcost.Text = netcost

    Return moviecost
    Return movietax

End Function

Public Function addMovie() As String




    movieChoice = lstCom.SelectedIndex

    For X = LBound(strMovies) To UBound(strMovies)
        If lstCom.SelectedIndex = X Then
            lstCom2.Items.Add(strMovies(X) & " $" + intMoviePrices(movieChoice).ToString)
            numberdvds += 1
            Call calculateTotals(movieChoice)

        End If
    Next

End Function

Public Function removeMovie() As String



    For X = LBound(strMovies) To UBound(strMovies)
        If lstCom2.SelectedIndex = X Then
            lstCom2.Items.Remove(lstCom2.SelectedItem)
            numberdvds -= 1
            Call calculateTotals(movieChoice)

        End If
    Next





End Function

Private Sub mainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load




    lstCom.Items.Add("Spider-Man")
    lstCom.Items.Add("Daredevil")
    lstCom.Items.Add("Hulk")
    lstCom.Items.Add("The Punisher")
    lstCom.Items.Add("Spider-Man 2")
    lstCom.Items.Add("Fantastic Four")
    lstCom.Items.Add("Spider-Man 3")
    lstCom.Items.Add("Iron Man")
    lstCom.Items.Add("The Amazing Spider-Man")
    lstCom.Items.Add("The Wolverine")
    lstCom.SelectedIndex = 0


End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Call addMovie()

End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

    Me.Close()

End Sub

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click

    Call removeMovie()
End Sub

结束班

2 个答案:

答案 0 :(得分:2)

您每次计算时都会重置影片成本,因为它仅与该功能相关。

移动它:

Dim moviecost As Integer

除了快速修复的功能之外,但是为了做到这一点,我会把它留在那里并且每次从篮子里计算,我之所以这样做是因为如果你从购物车中删除了某些东西,你就不会这样做。 t删除价格。最好在每次更改时根据购物车内容动态计算。

你也可以替换所有这些:

If lstCom.SelectedIndex = 0 Then
    lstCom2.Items.Add ("Spider-Man " + "$" + intMoviePrices(movieChoice).ToString)
    Call calculateTotals(movieChoice)
ElseIf lstCom.SelectedIndex = 1 Then
    lstCom2.Items.Add ("Daredevil " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 2 Then
    lstCom2.Items.Add ("Hulk " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 3 Then
    lstCom2.Items.Add ("The Punisher " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 4 Then
    lstCom2.Items.Add ("Spider-Man 2 " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 5 Then
    lstCom2.Items.Add ("Fantastic Four " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 6 Then
    lstCom2.Items.Add ("Spider-Man 3 " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 7 Then
    lstCom2.Items.Add ("Iron Man " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 8 Then
    lstCom2.Items.Add ("The Amazing Spider-Man " + "$" + intMoviePrices(movieChoice).ToString)
ElseIf lstCom.SelectedIndex = 9 Then
    lstCom2.Items.Add ("The Wolverine " + "$" + intMoviePrices(movieChoice).ToString)
End If

有了这个:

For X = LBound(strMovies) To UBound(strMovies)
    If lstCom.SelectedIndex = X Then
        lstCom2.Items.Add (strMovies(X) & " $" + intMoviePrices(movieChoice).ToString)
        Call calculateTotals(movieChoice)
    End If
Next

然后把它放在该函数的顶部:

Dim X As Integer

答案 1 :(得分:0)

添加另一个答案,因为这是第二个问题。我修改了你的代码,但因为我没有项目而无法测试。

请检查并报告回来。请注意,您需要在removeMovie例程中添加一些智能来计算成本和税,请参考我对addMovie函数所做的操作以获得一个想法。

计算总计例程基本上只是格式化并发布数据,实际数字是在添加/删除事物时计算出来的。我删除了传递给它的MovieChoice变量,因为它不再相关。

Public strMovies() As String =
        {"Spider-Man", "Daredevil", "Hulk", "The Punisher", "Spider-Man 2",
         "Fantastic Four", "Spider-Man 3", "Iron Man", "The Amazing Spider-Man", "The Wolverine"}
Public intMoviePrices() As Integer =
    {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Dim X As Integer
Dim Y As Integer
Dim moviecost As Integer
Dim movietax As Double
Dim numberdvds As Integer
Dim shippingcharge As Double
Dim netcost As Double
Dim movieChoice As String


Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstCom.SelectedIndexChanged

End Sub


Public Function calculateTotals() As Integer
    If numberdvds > 4 Then
        shippingcharge = 5
    Else
        shippingcharge = numberdvds
    End If
    netcost = movietax + moviecost + shippingcharge
    lblgrosscost.Text = moviecost
    lblsalestax.Text = FormatNumber(movietax, 2)
    lblshipping.Text = shippingcharge
    lblnetcost.Text = netcost
End Function

Public Function addMovie() As String
    movieChoice = lstCom.SelectedIndex
    moviecost = moviecost + intMoviePrices(movieChoice)
    movietax = movietax + (intMoviePrices(movieChoice) * 0.04)
    For X = LBound(strMovies) To UBound(strMovies)
        If lstCom.SelectedIndex = X Then
            lstCom2.Items.Add (strMovies(X) & " $" + intMoviePrices(movieChoice).ToString)
            numberdvds = numberdvds + 1
            Call calculateTotals()
        End If
    Next
End Function

Public Function removeMovie() As String
    'Need to add code to remove cost and tax here
    For X = LBound(strMovies) To UBound(strMovies)
        If lstCom2.SelectedIndex = X Then
            lstCom2.Items.Remove (lstCom2.SelectedItem)
            numberdvds = numberdvds - 1
            Call calculateTotals()
        End If
    Next
End Function

Private Sub mainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For X = LBound(strMovies) To UBound(strMovies)
        lstCom.Items.Add (strMovies(X))
    Next
End Function

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Call addMovie
End Function

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Function

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    Call removeMovie
End Function