I am trying to figure out how to write the code to change the number grade to a letter grade then count the letter grades and show how many of each letter in a list box. I need someone to explain this to me and explain what I am doing wrong. This is the code that I have so far and this is the assignment.

Twenty scores, each between 0 and 100, are given in a text file “data.txt”. Write a program that - reads scores from this file into an array when the form is loaded - compute and display the number of scores above average in a text box (when “Number of Scores Above Average” button is clicked) - displays all scores in a list box (when “Show All Scores” button is clicked) - displays the number of A’s (scores 90 to 100), B’s (scores 80 to 89), C’s (scores 70 to 79), D’s (scores 60 to 69), and F’s (59 and below) in another list box (when “Show Letter Grades” button is clicked)

Public Class Scores
Dim Scores() As String
Private Sub Scores_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Scores = IO.File.ReadAllLines("Scores.txt")
End Sub

Private Sub btnAbove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbove.Click
    Dim num As Integer = 0
    For i As Integer = 0 To Scores.Count - 1
        If CDbl(Scores(i)) >= 80 Then
            num += 1
        End If
    Next

    txtNumAbove.Text = " The Number Of Scores Above Average Is " & num

   End Sub

Private Sub btnShowAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAll.Click
    For Each score As String In Scores
        lstAllScores.Items.Add(score)
    Next
End Sub

Private Sub btnLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLetter.Click

    Dim letter As String = ""
    Dim count As Integer = 0
    For Each score2 As Integer In Scores
        If score2 >= 90 Then
            letter = "A"
        ElseIf score2 >= 80 Then
            letter = "B"
        ElseIf score2 > 70 Then
            letter = "C"
        ElseIf score2 > 60 Then
            letter = "D"
        ElseIf score2 < 59 Then
            letter = "F"
        End If
    Next
    For i As Integer = 0 To letter.Count - 1
        count += 1
    Next

    lstLetterGrade.Items.Add(letter)
End Sub

Your codes are quite right, but your conception is not clear how to maintain If ElseIf control flow. Your Logic is not clear in

If score2 >= 90 Then
    letter = "A"
ElseIf score2 >= 80 Then
    letter = "B"
ElseIf score2 > 70 Then
    letter = "C"
ElseIf score2 > 60 Then
    letter = "D"
ElseIf score2 < 59 Then
    letter = "F"
End If

Suppose, if you get "score2 = 95", what grade the variable "letter" will hold. It always holds the grade "D". It will check all first four conditions, because it satisfies all four conditions and finally the result is "D". Every value in the variable score2 holds is greater than 60 returns "D".
So you need a modification in this portion.

If score2 >= 90 Then    
    letter = "A"
ElseIf (score2 >= 80) And (score2 < 90) Then
    letter = "B"
ElseIf (score2 >= 70) And (score2 < 80) Then
    letter = "C"
ElseIf (score2 >= 60) And (score2 < 70) Then
    letter = "D"
ElseIf score2 <= 59 Then
    letter = "F"
End If

Do not understand why did you use the following

For i As Integer = 0 To letter.Count - 1
count += 1
Next

The codes does not satisfy your problem

the code to change the number grade to a letter grade then count the letter grades and show

Thanks

When I debug I am not getting an error code but the letter in the listbox is always B. I need the program to count how many A's and display the number of each letter grade like 5 A's 3 B's etc.

The problem is count for every grade and show it with grade letter in list box.
This is most simple matter. For thiis you need some modification. Like

Private Sub btnLetter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLetter.Click
        'Declare two Array 
        Dim letter(4) As String 'To store the Grade Letters
        Dim count(4) As Integer 'To store the counts of grade letters

        'Initializing the Grade Letter Array by the corresponding Letters
        letter(0) = "A"
        letter(1) = "B"
        letter(2) = "C"
        letter(3) = "D"
        letter(4) = "F"

        'Calculate and store the numbers in count Array for corresponding letters
        For Each score2 As Integer In Scores
            If score2 >= 90 Then
                count(0) += 1                               'For Grade "A"
            ElseIf (score2 >= 80) And (score2 < 90) Then
                count(1) += 1                               'For Grade "B"
            ElseIf (score2 >= 70) And (score2 < 80) Then
                count(2) += 1                               'For Grade "C"
            ElseIf (score2 >= 60) And (score2 < 70) Then
                count(3) += 1                               'For Grade "D"
            ElseIf score2 <= 59 Then
                count(4) += 1                               'For Grade "F"
            End If
        Next

        'Now time to display the Gradelist with their counts.
        For i As Integer = letter.GetLowerBound(0) To letter.GetUpperBound(0)
            If count(i) < 1 Then
                lstLetterGrade.Items.Add("NIL " & letter(i))
            ElseIf count(i) = 1 Then
                lstLetterGrade.Items.Add(CStr(count(i)) & " " & letter(i))
            ElseIf count(i) > 1 Then
                lstLetterGrade.Items.Add(CStr(count(i)) & "'s " & letter(i))
            End If

        Next

    End Sub

Hope, it would help you

Rather than using successive IF-THEN-ELSE I suggest the more concise

Select Case score
    Case 90 To 100
        letter = "A"
    Case 80 To 89
        letter = "B"
    Case 70 To 79
        letter = "C"
    Case 60 To 69
        letter = "D"
    Case Else
        letter = "F"
End Select
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.