Vb.net Net中断Foreach循环
如何生成这样的输出: 英国人詹姆斯·摩尔、数学人卡伦·哈特斯金、科学人昆尼·奥普 使用foreach循环 我有这个密码-Vb.net Net中断Foreach循环,vb.net,Vb.net,如何生成这样的输出: 英国人詹姆斯·摩尔、数学人卡伦·哈特斯金、科学人昆尼·奥普 使用foreach循环 我有这个密码- dim T.Items.Add("James Morre","Karen Hatskin","Quennie Orph") dim S.Items.Add("English","Math","Science") For Each teacherItem as string In T.Items For Each subjectItem as string In S.It
dim T.Items.Add("James Morre","Karen Hatskin","Quennie Orph")
dim S.Items.Add("English","Math","Science")
For Each teacherItem as string In T.Items
For Each subjectItem as string In S.Items
Next
Next
更新:
For Each teacherItem As String In tSub.Items
Try
For Each subjectItem As String In avSub.Items
Using cmd As New SqlClient.SqlCommand("insert into Student_Grade values('" & txt_lrn2.Text & "','" & txt_name.Text & "','" & txtgrade.Text & "','" & txtsection.Text & "','" & subjectItem.ToString & "','" & teacherItem.ToString & "',0,0,0,0,0,'" & SY.Text & "')", cn)
x = cmd.ExecuteNonQuery
End Using
Next
Catch ex As Exception
Continue For
End Try
Next
但是我下面的输出是多余的
试试这个:
Dim T = {"James Morre", "Karen Hatskin", "Quennie Orph"}
Dim S = {"English", "Math", "Science"}
Dim n = Math.Min(T.Length, S.Length)
For i = 0 To n - 1
Console.Write(S(i) & " " & T(i))
If i < n - 1 Then
Console.Write(", ")
End If
Next
不要对每个循环使用嵌套。使用单个For循环,然后将循环计数器用作两个集合的索引。如何获取两个集合的索引?有样本代码吗?你没有得到任何集合的索引。循环计数器是索引。如果您无法找到如何通过索引从集合中获取项,则您不会尝试。要对两个集合执行此操作,只需执行两次。如果使用有效的VB.NET语法,那将非常棒。也许我的代码太复杂了,但我只想得到上面提到的输出。为了更好地参考,我已更改了我的问题。 English James Morre, Math Karen Hatskin, Science Quennie Orph
Console.WriteLine(String.Join(", ", S.Zip(T, Function (x, y) x & " " & y)))