2017-10-17 46 views
0

针对此特定问题的练习是,我必须根据列表框中选定的成绩值显示学生数量,并显示学生人数数字标签中的选定等级:Form Design正在搜索名称和成绩的字符串数组以及与成绩相关的列表名称

我可以为这些年级添加数字,我一直在跑的问题是同时在Name数组中搜索Name数组,并根据所选年级获取每个单独的名称以显示出来。

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

编辑:这正是分配呼吁:

一个。该过程声明并初始化两个并行的名为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 
+0

请仔细阅读[提问]和取[旅游]。如果您使用'For n'循环,索引器('n')将指向相应的名称数组(或者您可以使用Array.IndexOf)。据推测,您将需要另一个集合来存储每个字母等级的名称 – Plutonix

+0

我无法使用Array.IndexOf,因此我的意思是我必须在不使用它的情况下完成此练习。 – user3472383

+0

请阅读[Ask]并参加[tour] – Plutonix

回答

0

我填在ABCDF' 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 
+0

谢谢!这正是我所坚持的。 我想我自己勉强自己,因为我想使用Select Case语句,但是一个简单的If/Else语句似乎完成了这个诀窍。 – user3472383

0

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

例如创建一个类学生,与属性:名称,等级,然后分配值给每个对象为每个学生,然后将它们添加到列表中。然后在该列表中,您可以编写一个LINQ查询。

您可以了解更多关于如何做到这一点,在这里:如果你想只使用数组然后更改foreach循环的循环,这样做https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/linq/basic-query-operations

编辑

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))

+0

列表(T)是一个更好的选择,但我确信这是与数组一起工作的作业 – Plutonix

+1

请阅读此处。你可能会发现它具有洞察力。[致家庭作业问题的学生的公开信](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) –

+0

这是不是家庭作业,而是本章末尾对我的Visual Basic书的练习。我已经重新添加了练习在我的主要帖子中所要求的内容。 – user3472383