Unity C# Basics — Letter Grade Calculator

Ryan McCoach
3 min readAug 11, 2021

--

In this Unity C# Basics series we are going to create a Letter Grade Calculator. The program will first add all of the quiz scores together, divide by the amount of quizzes taken to get the average quiz score. Once the average quiz score has been calculated, we will perform a check to see what letter grade range it fells into and print that letter grade. The concept we are targeting in this mini project are the use of If— Else If — Else conditionals.

Lets declare the variables we are going to use…

_quizScores = is an array that will hold each of the individual quiz scores. It is going to be a whole number so we will use an integer variable type.

_quizScoresTotal = is the sum of all of the individual quiz scores. This will be a whole since all of the quiz scores are whole numbers, so it will be an int variable.

_average = is the quotient of the quiz score total divided by the total amount of quizzes taken.

_letterGrade = is the letter grade that will be assigned depending on the average. Since it is a letter this will need to be a string variable type.

The Code Breakdown

In On Start, we use a For Loop and to add all of the Quiz Scores together to get the Quiz Scores Total. I am not covering For Loops in this article.

Now that we have the Quiz Scores Total, we can find the average by dividing the Quiz Scores Total by the amount of Quiz Scores in the array. We will need to round to the closest whole number, so we will use the Math Function.Round.

Depending on the average score will determine the Letter Grade to print in the Inspector. This is where If — Else If — Else conditionals come into play. IF the average is greater than or equal to 90, we print the Letter Grade of “A”. We several other ranges we need to check, so we can use Else If to see if the average falls between 80–89, 70–79, 60–69. In creating a these ranges we use an AND operator that (&&) requires both conditions to be true in order to print the matching Letter Grade. The last condition is Else, which takes any average score that is 59 or less. We do not need to be a conditional statement since it handles every ELSE that wasn’t in the If and Else If statements. There you have — a basic Letter Grade Calculator.

The Code

--

--

No responses yet