针对此特定问题的练习是,我必须根据列表框中所选的成绩值显示学生数量,并在数字标签中显示所选成绩的学生数量:Form Design。
我可以为成绩添加数字就好了,我遇到的问题是在等级数组的同时搜索Name数组,并根据所选的等级显示每个单独的名称。
我知道成绩字母的每个索引值都对应于Name数组,但我不知道如何获得Grade数组的Index值,因为它是一个字符串。
编辑:这正是作业所要求的:
一个。该过程声明并初始化两个名为strNames和strGrades的并行一维数组 对程序进行编码,以显示已获得在lstGrades控件中选择的成绩的学生的姓名。它还应显示获得该成绩的学生人数。
湾当界面出现时,应选择lstGrades控件中的第一项。编写适当的程序。
℃。当在lstGrades控件中选择了不同的等级时,应清除lstNames和lblNumber控件的内容。编写适当的程序。
d。保存解决方案,然后启动并测试应用程序。
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' Display the names and number of students earning a specific grade.
Dim strNames() As String = {"Helen", "Peter", "Yolanda", "Carl", "Jennifer", "Charles", "Addison", "Aiden", "Treyson", "Sydney", "Jacob", "Nancy", "George", "Ursula", "Jack"}
Dim strGrades() As String = {"A", "B", "B", "A", "D", "F", "A", "B", "A", "B", "F", "C", "C", "B", "D"}
Dim intNumGrades(4) As Integer
' searches through each value in strGrade array, counter is added for each instance
For Each strGradeLetter As String In strGrades
Select Case strGradeLetter
Case "A"
intNumGrades(0) += 1
Case "B"
intNumGrades(1) += 1
Case "C"
intNumGrades(2) += 1
Case "D"
intNumGrades(3) += 1
Case "F"
intNumGrades(4) += 1
End Select
Next strGradeLetter
lblNumber.Text = intNumGrades(lstGrades.SelectedIndex).ToString
End Sub
答案 0 :(得分:0)
不是创建字符串数组,而是创建自己定义的类的列表。
创建一个类Student,例如,使用属性:Name,Grade,然后为每个学生的每个对象分配值,然后将它们添加到列表中。然后在该列表上,您可以编写LINQ查询。
您可以在此处详细了解如何执行此操作:https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/linq/basic-query-operations
修改强>
如果你只想使用数组,那么将foreach循环更改为for循环,并执行以下操作:
For index As Integer = 0 To (strGrades.Length -1)
Select Case strGrades(index)
Case "A"
intNumGrades(0) += 1
Case "B"
intNumGrades(1) += 1
Case "C"
intNumGrades(2) += 1
Case "D"
intNumGrades(3) += 1
Case "F"
intNumGrades(4) += 1
End Select
Next
您可以使用索引访问其他相应的数组。
如果您只想显示它们,那么您可以将所有名称附加到字符串例如:如果是“A”gradeAStudent += strNames(index) + " "
或者,您可以将它们附加到列表中:
studentList.Add(strNames(index))
答案 1 :(得分:0)
我在设计的时间用A B C D F'填写了成绩列表框。 lbStudents是一个列表框。 问题是什么叫lstGrades我称之为lbGrades。没有不同 名字除外。
Public Class Form1
Private Sub lbGrades_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbGrades.SelectedIndexChanged
'Clear the list before adding new students
lbStudents.Items.Clear()
Dim strNames() As String = {"Helen", "Peter", "Yolanda", "Carl", "Jennifer", "Charles", "Addison", "Aiden", "Treyson", "Sydney", "Jacob", "Nancy", "George", "Ursula", "Jack"}
Dim strGrades() As String = {"A", "B", "B", "A", "D", "F", "A", "B", "A", "B", "F", "C", "C", "B", "D"}
'Using a variable for the size of your arrays makes the code more generic so it could be
'used for different size arrays.
Dim n As Integer = strGrades.Count - 1
Dim itgCount As Integer = 0
'This and the matching .EndUpdate prevents the list box from repainting on each iteration
'Not important in this example but could be very important for long list additions
lbStudents.BeginUpdate()
'loop through the idexes of the arrays
'VB arrays are zero based = the first element is index 0
For index As Integer = 0 To n
'Check the grades array for the item selected in the list box
If strGrades(index) = lbGrades.SelectedItem Then
'if found, increment the counter
itgCount += 1
'find the name of the student by using the same index as the grades array
'this is the value of parallel arrays
'Add the name to the other list box
lbStudents.Items.Add(strNames(index))
End If
Next
lbStudents.EndUpdate()
'Display the count in a label using an interpolated string
lblCount.Text = $"The number of students with Grade {lbGrades.SelectedItem} is {CStr(itgCount)}."
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This satisfies the requirement in b. of the problem
lbGrades.SelectedItem = "A"
End Sub
End Class