English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I am writing I program in Visual Basic I am suppose to calculate the average of three test. After I get the average I am suppose to give a letter grade on the application.

'calulates the grade from three test for the students in Professor Juarez class
'declare variables
Dim Test1 As Integer
Dim Test2 As Integer
Dim Test3 As Integer
Dim Avg As Integer
Dim Grade As Char

'convert input to a letter


'calculate the grade from the test scores
Avg = (Test1 + Test2 + Test3) / 3

If Avg >= 90 Then
Grade = "A"
Else
If Avg > 80 And Avg <= 89 Then
Grade = "B"
Else
If Avg > 70 And Avg <= 79 Then
Grade = "C"
Else
If Avg > 60 And Avg <= 69
Grade = "D"
Else
If Avg < 60 Then
Grade = "F"

End If
End If
End If
End If
End If

'display the letter grade for the average

GradeLab.Text = Convert.ToString(Grade)

End Sub

2007-03-15 15:38:22 · 4 answers · asked by lil' Mama 1 in Computers & Internet Programming & Design

4 answers

Everything in that code is correct except the way the If..End If, is coded.
It should be

Avg = (Test1 + Test2 + Test3) / 3

If Avg >= 90 Then
Grade = "A"
ElseIf Avg > 80 And Avg <= 89 Then
Grade = "B"
ElseIf Avg > 70 And Avg <= 79 Then
Grade = "C"
ElseIf Avg > 60 And Avg <= 69
Grade = "D"
Else Avg < 60 Then
Grade = "F"

End If

The last "Else" you do not need a "ElseIf" because if any of the numbers do not fall in any of those A, B, C, or D catagory, it has to be a "F".

It is good that you are converting "Grade" to a string. At the very top instead of "as Character" you should set the "Grade" to a string value.

And to help you out a little.

Another way to write "Convert.ToString()" is "CStr()"
Instead of this

Dim Test1 As Integer
Dim Test2 As Integer
Dim Test3 As Integer
Dim Avg As Integer
Dim Grade As Char

YOU can also do this

Dim Test1, Test2, Test3 as Integer

YOU can also merge many variables of differant types like this.

Dim strName as String, intGrade as Integer

2007-03-17 09:21:43 · answer #1 · answered by Anonymous · 0 0

your logic is ok. but syntex in VB is wrong.

once u find average then

public sub function avgGrade()
if avg>=90 then
Grade="A"
elseif avg > 80 and avg <89 then
Grade ="B"
---
---
---
end if

end sub

2007-03-15 22:31:26 · answer #2 · answered by Kannan 2 · 0 0

your good judgment is okay. yet syntex in VB is faulty. as quickly as u locate standard then public sub function avgGrade() if avg>=ninety then Grade="A" elseif avg > eighty and avg <89 then Grade ="B" --- --- --- end if end sub

2016-10-02 05:01:52 · answer #3 · answered by ? 4 · 0 0

Check out http://www.a1vbcode.com or http://www.pscode.com for great sample codes.

2007-03-15 15:44:26 · answer #4 · answered by Richard H 7 · 0 0

fedest.com, questions and answers