[英]Visual Basic Movie Program
I'm having a weird issue with this VB program I'm working on for class. 我正在上课的VB程序出现一个奇怪的问题。 I'm honestly not entirely sure why I'm running into the problem, because I've done other assignments with ease that were relatively similar to this. 老实说,我不确定自己为什么会遇到这个问题,因为我已经轻松地完成了与此类似的其他任务。 The program is designed to allow me to add certain movies from the store (left list box) to the cart (the right list box.) You can see that I added Spider-Man two times (because who doesn't like Spider-Man?) and it did correctly show the name and the $2 each time. 该程序旨在允许我将商店(左侧列表框)中的某些电影添加到购物车(右侧列表框)。您可以看到我两次添加了蜘蛛侠(因为谁不喜欢蜘蛛侠) ?),并且每次正确显示名称和$ 2。 However the first label box in the bottom should be accumulating each time I add the movie to the box, but it stays firm at the first movie I pick. 但是,每次将影片添加到该框时,底部的第一个标签框都应该累积,但在我选择的第一部影片中,该框保持固定。 If I were to pick any of the other movies, which are all more expensive, it will still stay at the first movie I picked. 如果我要选择其他任何一部更昂贵的电影,它将仍然停留在我选择的第一部电影中。
EDIT: I should add, I was having the same issue before I moved some parts of the code into their respective functions. 编辑:我应该补充,在将代码的某些部分移入各自的功能之前,我遇到了同样的问题。
Public Class mainForm 公共类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
End Class 末级
You are resetting movie cost each time you calculate as it is relative to that function only. 每次计算时都将重置电影成本,因为它仅与该功能有关。
Move this: 移动这个:
Dim moviecost As Integer
Outside of the function for a quick fix but to do it properly I would leave it in there and calculate from the basket every time, the reason I would do this is because if you remove something from the cart you don't remove the price. 在功能之外,可以快速解决问题,但要正确执行操作,我会每次都将其留在那儿并从购物篮中进行计算,之所以这样做,是因为如果您从购物车中取出物品,则不会除去价格。 Best to dynamically calculate based on the cart contents every time a change is made. 每次进行更改时,最好根据购物车内容进行动态计算。
Also you can replace ALL of this: 您也可以替换所有这些:
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
With this: 有了这个:
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
Then put this at the top of that function: 然后将其放在该函数的顶部:
Dim X As Integer
Adding another answer as this is the second question. 添加另一个答案,因为这是第二个问题。 I have modified your code but can't test as I don't have the project. 我已经修改了您的代码,但由于没有项目,因此无法进行测试。
Please check it out and report back. 请检查并报告。 Note you need to put some smarts into the removeMovie routine to work out the cost and tax, refer to what I did to the addMovie function to get an idea. 请注意,您需要在removeMovie例程中添加一些技巧,以计算出成本和税金,请参考我对addMovie函数所做的操作以了解想法。
The calculate totals routine basically just formats and posts the data, the actual numbers are worked out when things are added / remove. 计算总数例程基本上只是格式化和发布数据,添加/删除内容时计算出实际数字。 I have removed the MovieChoice variable being passed into it as it is no longer relevant. 我已经删除了传递给它的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.