如何制作这样的输出:
英语James Moore,数学凯伦哈特金,科学Quennie Orph (使用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.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
但我的输出是如此多余。
答案 0 :(得分:1)
试试这个:
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
这给出了:
English James Morre, Math Karen Hatskin, Science Quennie Orph
或者,不使用任何循环,这也适用:
Console.WriteLine(String.Join(", ", S.Zip(T, Function (x, y) x & " " & y)))