arrays - 我相信我做了一个无限循环,但我不知道它是在哪里创建的
问题描述
我正在尝试通过创建用户可以为商店创建商品列表的东西来练习数组。我希望项目名称转到一个列表框和一个数组,该数组将与一个包含项目价格和数量的数组平行。我还希望用户能够通过在列表框中选择项目并按下检查项目按钮来检查项目的价格和数量。当我尝试使用检查项目按钮时程序崩溃。
这是用户在他们的项目中添加的部分。
Private Sub btnAdd_click(sender As Object, e As EventArgs) Handles btnAdd.Click
addName = InputBox("What is the name of the product you wish to add?")
addPrice = InputBox("What is the price of the product?")
addQuantity = InputBox("What is the quantity in stock of the product?")
lstStock.Items.Add(addName)
End Sub
我将项目添加到数组中,我希望我将其设置为无论长度如何都会自动创建数组的位置。我相信这可能会创建一个冻结程序的无限循环。
Public Sub added(ByRef addlist As String, ByRef addPQ As String)
Dim nameID As Double = lstStock.Items.Count
Dim lstCount As Double = lstStock.Items.Count
Dim lstArrayCount As Double = 0
Dim num As Double
Dim lstNum As String
While lstArrayCount < 1
lstNum = addName
lstArrayCount = +1
End While
While lstArrayCount < lstCount
lstNum = lstNum & addName & "}, {"
lstArrayCount = +1
End While
Dim priceQuantityCount As Double
Dim lstPrice As String
While priceQuantityCount < 1
lstPrice = lstPrice & addPrice & ", " & addQuantity & "}"
priceQuantityCount = +1
End While
While priceQuantityCount > 1 And priceQuantityCount < lstCount
lstPrice = lstPrice & ", {" & addPrice & ", " & addQuantity & "}"
priceQuantityCount = +1
End While
Dim List() As String = {lstNum}
Dim PriceQuantity(,) As String = {{lstPrice}}
addlist = List.ToString()
addPQ = PriceQuantity.ToString()
End Sub
按下按钮时,一切都冻结了。为了以防万一,我把它放在这里。除了程序冻结并且 cpu 使用率上升之外,没有任何视觉效果发生。没有错误消息或类似的东西。
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
Dim check As String = lstStock.SelectedItem.ToString()
Dim add1 As String
Dim add2 As String
added(add1, add2)
Dim strPrice As String
Dim strQuantity As String
Dim PQArray As String
Dim intIndex As Integer
PQArray = add2
intIndex = PQArray.IndexOf("")
If intIndex <> -1 Then
strPrice = PQArray.Substring(0, intIndex)
strQuantity = PQArray.Substring(intIndex + 1)
End If
MessageBox.Show("there are " & "quantity" & " " & check & " in stock priced at $" & "price" & " each.")
MessageBox.Show(strPrice & " : " & strQuantity)
End Sub
结束类
解决方案
让我们从三个数组开始。
一个名称数组。这将保留在列表框本身中。是ListBox.Items
一个对象数组。
表单级别的价格数组。这应该是类型Decimal
- 好用于金钱的东西。
Private PriceArray(10) As Decimal
表单级别的 Quantity 数组。这应该是整数类型,库存项目的整数。
Private QuantityArray(10) As Integer
现在让我们看看您的 Add 方法。
Private Sub btnAdd_click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim addName = InputBox("What is the name of the product you wish to add?")
Dim addPrice = InputBox("What is the price of the product?")
Dim addQuantity = InputBox("What is the quantity in stock of the product?")
ListBox1.Items.Add(addName)
PriceArray(CurrentIndex) = CDec(addPrice)
QuantityArray(CurrentIndex) = CInt(addQuantity)
CurrentIndex += 1
End Sub
我将您的 3 个变量更改为局部变量(添加了 Dim)。我更改了列表框的名称,以便它可以在我的测试项目中使用。就像您一样,我将名称添加到列表框中。第一次单击此按钮时,名称将添加到索引 0 处的 Items 集合中。我添加了另一个表单级变量CurrentIndex
. 这将跟踪我们在数组中的位置。第一次通过CurrentIndex
默认初始化为0
. 我们使用 Current Index 作为我们填写输入值的索引。下次我们添加时,我们需要递增CurrentIndex
到下一个索引。
CurrentIndex += 1
我认为你试图这样做,但你的语法有点错误。它是 += ; 你有 = +1 只会将 1 分配给变量。
让我们想想我们在哪里。列表框中的名称具有与价格数组和数量数组中的索引匹配的索引。数组是并行的(并发的)。
为了从我们的数组中获取信息,我使用了SelectedIndexChanged
事件。
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim IndexSelected = ListBox1.SelectedIndex
TextBox1.Text = PriceArray(IndexSelected).ToString("N2")
TextBox2.Text = QuantityArray(IndexSelected).ToString
End Sub
我们可以通过.SelectedIndex
属性获取索引。我们使用该索引来查找 和 中的匹配PriceArray
值QuantityArray
。的"N2"
后面告诉它显示一个小数点后 2 位的数字.ToString
。PriceArray
推荐阅读
- swagger - 为 OpenApi 中的全局响应引用 Schema 中的现有类型
- python - python检查第一列名并从excel中检索输入
- testing - 异常 SpecFlow.Plus.Shared.Services.Activation.ActivationException
- macos - iTerm2 日志轮换
- tabs - 有没有办法垂直堆叠“标签”?
- ssl - 客户端通过 ROOT CA 签名的证书进行身份验证
- mysql - 使用 Jenkins 时与 MySQL 服务器 docker 容器的连接丢失
- telerik - Telerik RadTextBox XSS 过滤器问题
- c++ - png 未在 SDL 中正确呈现
- bash - 在 Ubuntu 中使用 bashrc 定义带循环的别名