首页 > 解决方案 > 在循环中显示来自两个数组的 MsgBox() 数据

问题描述

我正在尝试编写代码以通过 MsgBox() 显示来自两个数组的数据。我有下面的代码,但当然它不起作用:

Dim numbers() As Integer = {1, 4, 7}
Dim letters() As String = {"a", "b", "c"}
' Iterate through the list by using nested loops.
For Each number As Integer In numbers and For Each letter As String In letters
   MsgBox(number.ToString & letter & " ")
Next

我需要做什么才能获得这样的输出?:

1a
4b
7c

标签: vb.net

解决方案


您需要一个For使用索引而不是循环的For Each循环:

Dim numbers() As Integer = {1, 4, 7}
Dim letters() As String = {"a", "b", "c"}

For i As Integer = 0 To numbers.Length - 1
   MsgBox(numbers(i) & letters(i))
Next

您还可以使用Zip() linq 运算符

For Each output As String In numbers.Zip(letters, Function(n, l) n & l)
    MsgBox(output)
Next

推荐阅读