Hello everyone.

I have been struggling with my programming class from day one. I still can't seem to grasp the complete logic on how to write code. Anyhow, I have an assignment due that I am stuck on. Here is the assignment:

In this exercise, you code an application that displays a letter grade based on the
average of three test scores. Open the Grade Solution (Grade Solution.sln) file,
which is contained in the ClearlyVB\ Chapl8\Grade Solution folder. Code the application,
using a function to determine and return the letter grade. If the average is at
least 90, the grade is A. If the average is at least 80 but less than 90, the grade is B.
If the average is at least 70 but less than 80, the grade is C. Tf the average is at least
60 but less than 70, the grade is D. If the average is below 60, the grade is F.

You will be developing the code for the function on your own. Below the Public Class frmMain line, insert this function header:

Private Function CalculateGrade(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, ByVal testscore3 As Decimal) As String

In the function, declare variables for the average score and the letter grade. Calculate the average, use IF/Elself statements to determine the letter grade, and return the letter grade. Create a subprocedure for the Click event for the btnDisplay button. It should call the CalculateGrade function with three arguments ( the three test scores from the text boxes), and display the returned value in the lblGrade box.

This is what I have so far:

Public Class frmMain

    Private Function CalculateGrade(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, ByVal testScore3 As Decimal) As String

        Dim AverageScore As String
        Dim strLetterGrade As Object
        Dim intNum1 As Integer
        Dim intNum2 As Integer
        Dim intNum3 As Integer
        Dim intNum4 As Integer

        AverageScore = (testScore1 + testScore2 + testScore3) / 3
        strLetterGrade = {"A" , "B" , "C" , "D" , "F"}
        intNum1 = 90
        intNum2 = 80
        intNum3 = 70
        intNum4 = 60


        If AverageScore >= 90 Then
            Return strLetterGrade("A")

        ElseIf AverageScore >= 80 Then
            Return strLetterGrade("B")

        ElseIf AverageScore >= 70 Then
            Return strLetterGrade("C")

        ElseIf AverageScore >= 60 Then
            Return strLetterGrade("D")

        ElseIf AverageScore < 60 Then
            Return strLetterGrade("F")
        End If

        Return strLetterGrade

    End Function

    
    Dim testScore1 As Decimal
    Dim testScore2 As Decimal
    Dim testScore3 As Decimal
    Dim AverageScore As String = (testScore1 + testScore2 + testScore3) / 3


    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        ' Display the average score

        lblGrade.Text = AverageScore

    End Sub

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub


    Private Sub txtTest1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTest1.KeyPress, _
    txtTest2.KeyPress, txtTest3.KeyPress
        ' allows the text box to accept only numbers, the period, and the Backspace key

        If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> "." _
            AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If
    End Sub


    Private Sub txtTest1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTest1.TextChanged, _
     txtTest2.TextChanged, txtTest3.TextChanged

        lblGrade.Text = String.Empty
    End Sub
End Class

I am going to have to re-study this material over the summer to try and understand it better, but for now I could use any suggestions that are offered.

Thank you,

Terry

jlego commented: atleast you tried to do this on your own and posted the code +3
Option Strict On

Public Class frmMain

    Private Function AverageScore(ByVal testscore1 As Decimal, ByVal testscore2 As Decimal, ByVal testscore3 As Decimal) As Decimal
        Return (testscore1 + testscore2 + testscore3) / 3
    End Function

    Private Function CalculateGrade() As String

        If CDec(lblGrade.Text) >= 90 Then
            Return "A"
        ElseIf CDec(lblGrade.Text) >= 80 Then
            Return "B"

        ElseIf CDec(lblGrade.Text) >= 70 Then
            Return "C"

        ElseIf CDec(lblGrade.Text) >= 60 Then
            Return "D"

        ElseIf CDec(lblGrade.Text) < 60 Then
            Return "F"
        End If

        Return "Unknown?"
    End Function

    Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        lblGrade.Text = CInt(AverageScore(CDec(Me.txtTest1.Text), CDec(Me.txtTest2.Text), CDec(Me.txtTest3.Text))).ToString
        MsgBox(CalculateGrade)
    End Sub


    Private Sub txtTest1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTest1.KeyPress, txtTest2.KeyPress, txtTest3.KeyPress
        ' allows the text box to accept only numbers, the period, and the Backspace key
        If Not Char.IsNumber(e.KeyChar) Then e.KeyChar = Nothing
    End Sub

    Private Sub txtTest1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTest1.TextChanged, txtTest2.TextChanged, txtTest3.TextChanged
        lblGrade.Text = String.Empty
    End Sub
End Class

copy and paste that into your form

wow not even a thanks :|

We are happy.... THANK YOU!

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.