vb.net - 在循环中显示来自两个数组的 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
解决方案
您需要一个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
推荐阅读
- css - Bootstrap 5:变量覆盖不起作用
- scala - 你如何在特质中超载应用?
- python - Kivy:AttributeError:'str'对象没有属性'text'
- php - Shopify-Bridge 调用未定义的函数 getShopifyConfig()
- git - 我可以在本地将 git symbolic-ref 设置为远程分支吗?
- r - 如何在数据帧的每一行上运行函数,同时使用该数据帧中的多个参数,并在列表中输出新的数据帧
- system-design - 如何计算需要的消息队列工作人员的数量?
- javascript - 是否有可能打开一个模式?
- reactjs - 具有一些依赖关系的反应`useReducer`
- r - rbind.fill 的最快替代方案