【问题标题】:Searching a string array of names and grades and listing names associated with grade搜索名称和等级的字符串数组并列出与等级相关的名称
【发布时间】:2017-10-17 17:56:41
【问题描述】:

针对这个特定问题的练习是,我必须在列表框中显示所选年级值的学生数量,并在数字标签中显示所选年级的学生人数:Form Design

我可以将分数加起来就好了,我一直遇到的问题是在搜索名称数组的同时搜索等级数组,并根据所选等级显示每个单独的名称。

我知道 Grade 字母的每个索引值都会对应 Name 数组,但我不知道如何获取 Grade 数组的 Index 值,因为它是一个字符串。

编辑:这正是任务所要求的:

一个。该过程声明并初始化了两个名为 strNames 和 strGrades 的并行一维数组。
对过程进行编码,以显示获得 lstGrades 控件中所选成绩的学生的姓名。它还应该显示获得该成绩的学生人数。

b.出现界面时应选择 lstGrades 控件中的第一项。编码适当的程序。

c。当在 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

【问题讨论】:

  • 请阅读How to Ask 并使用tour。如果您使用了For n 循环,则索引器 (n) 将指向相应的名称数组(或者您可以使用 Array.IndexOf)。大概您将需要另一个集合来存储每个字母等级的名称
  • 我不能使用 Array.IndexOf,我的意思是我必须在不使用它的情况下完成这个练习。
  • 请阅读How to Ask 并采取tour

标签: arrays vb.net loops


【解决方案1】:

我在设计时间用 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

【讨论】:

  • 谢谢!这正是我所坚持的。我想我自欺欺人是因为我想使用 Select Case 语句,但一个简单的 If/Else 语句似乎已经成功了。
【解决方案2】:

创建一个您自己定义的类的列表,而不是创建字符串数组。

例如,创建一个学生类,其属性为:姓名、成绩,然后将值分配给每个学生的每个对象,然后将它们添加到列表中。然后在该列表上,您可以编写一个 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))

【讨论】:

  • A List(of T) 是一个更好的选择,但我很确定这是处理数组的功课
  • 在这里阅读。你可能会发现它很有见地..Open letter to Students with homework problems
  • 这不是家庭作业,而是我的 Visual Basic 书的章节末尾的练习。我已经在我的主要帖子中重新添加了练习所要求的内容。
  • 这个问题有点乱码和不清楚,但我想她也想收集那些根据the problem I keep running into is searching the Name array at the same time得到每个字母等级的人的名字
  • @Plutonix 因为在 for 循环中,他/她将拥有她所在的等级数组中哪个元素的索引,她可以使用它来获取该索引处的名称,例如:strNames(index) 并将其用作他/她想要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
  • 2016-04-13
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多