2017-10-17 69 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