【问题标题】:Visual Basic Movie ProgramVisual Basic 电影程序
【发布时间】:2015-04-16 11:46:39
【问题描述】:

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

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

公共类主窗体

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

结束类

【问题讨论】:

  • 您可能希望将标签从 VBA 更改为 VB.Net 或您正在使用的 Visual Basic 衍生版本,这不是 VBA 形式:)。
  • @Dan Donoghue 可能是在服用类固醇...
  • 谢谢你,丹,这可能是我得到反对票的原因,或者因为这是一个愚蠢的问题。感谢您的帮助,我目前正在玩弄您的答案!
  • addMovie 中,您需要将呼叫转移到IF 块之外的calculateTotals(movieChoice)。目前你所有的电影都是免费的,除了蜘蛛侠
  • @Baby 你应该被放在角落里说这样的话!

标签: vb.net


【解决方案1】:

您每次计算时都会重置电影成本,因为它仅与该函数相关。

移动这个:

Dim moviecost As Integer

在功能之外进行快速修复,但要正确执行,我会将其留在其中并每次从篮子中计算,我这样做的原因是因为如果您从购物车中删除某些东西,您不会删除价格。每次更改时最好根据购物车内容动态计算。

您也可以替换所有这些:

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) 使 MovieCost 成为模块级变量(将其移到任何函数或子程序之外) 2) 添加电影时执行此操作:moviecost = moviecost + intMoviePrices(movieChoice) 3) 删除电影时,执行此操作:moviecost = moviecost - intMoviePrices(电影选择)。应该为您解决所有问题。最后,当您更改 MovieCost 时,请始终执行此 lblgrosscost.Text = moviecost
  • 还要检查我在回答中的编辑。如果为了你,我砍掉了这么大的东西。
  • 太棒了。我实际上已经有了节省时间的想法,但是现在我担心的是,当我用这个来计算税收、运费和净总额时,如果我删除任何东西,它真的会搞砸。或者也许只是在每次删除后调用计算仍然可以?至于您的编辑,非常感谢。
  • 不用担心,随时乐意为您提供帮助。有了你的税,你的想法是正确的,每次你更新moviecost时计算它,当你从moviecost中添加或删除时,你将能够计算出从Tax中添加或删除的内容,请记住,你还需要Tax变量模块级别。
【解决方案2】:

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

请检查并报告。请注意,您需要在 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

【讨论】:

  • 你是我的英雄。我不仅了解您的更改,而且能够操纵您必须做的事情来做我想做的事情,并完成项目。我不能再欣赏你的时间了!!!!!!
  • @CullinMcGrath 你可以询问他的银行账户号码,或者可能要邮寄一打啤酒的地址:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
  • 2012-01-20
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
相关资源
最近更新 更多