Do ...使用数组为列表框循环语句

时间:2015-01-23 18:50:01

标签: arrays vb.net

在第一部分遇到问题时,我将第二部分全部写在纸上,但我无法在没有第一部分的情况下将其插入。

我遇到的是btnAdd_Click程序应该在intQuantities数组中为每个元素添加数字1(我已经拥有了我想要做的元素),并在数组中显示数组的内容lstQuantities控制;使用Do ... Loop语句。

'类级数组     Private intQuantities()As Integer = {45,67,2,5,90}

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

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    ' adds 1 to each array value and then displays the array




    lstQuantities.Items.Clear()

End Sub

Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
    ' subtracts 1 from each array value and then displays the array

    lstQuantities.Items.Clear()

我只是无法抓住这个概念并且难以使用Do ...循环,For ...下一个陈述是我为减法按钮所做的。

2 个答案:

答案 0 :(得分:0)

使用Do...Loop迭代数组有点不寻常,但您需要做的就是在循环内增加一个索引变量,直到它等于intQuantities数组的长度。您可以使用Do...Loop的任何一种形式 - 例如使用While

Dim i = 0
Do While i < intQuantities.Length
    intQuantities(i) += 1
    i += 1
Loop

或使用Until

Dim i = 0
Do Until i = intQuantities.Length
    intQuantities(i) += 1
    i += 1
Loop

答案 1 :(得分:0)

  

是否要永久更改原始值   数组?...或只在ListBox中显示值+ 1? - Idle_Mind

     

2天前在我的列表框中显示值+ 1 - 泰勒

这是做加一减零件的一种方法:

Public Class Form1

    Private intQuantities() As Integer = {45, 67, 2, 5, 90}

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        lstQuantities.DataSource = Array.ConvertAll(intQuantities, Function(x) x + 1)
    End Sub

    Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
        lstQuantities.DataSource = Array.ConvertAll(intQuantities, Function(x) x - 1)
    End Sub

End Class