Reputation: 41
I attend an online c# class and it asked me to do this:
Your solution can use a series of branching statements like if(), else-if(), and else(), but does not require any iterative loops.
The grade conversion levels should be as follows:
- A grade of "A" is given to any value between 90 - 100
- A grade of "B" is given to any value between 80 - 89
- A grade of "C" is given to any value between 70 - 79
- A grade of "D" is given to any value between 60 - 69
- All other values will receive a grade of "F"
My code is
private char convertToLetterGrade(int numberGrade)
{
// replace with student code(Which I have done)
if (numberGrade >= 90 && numberGrade <= 100)
{
MessageBox.Show("A");
}
else if (numberGrade >= 80 && numberGrade <= 90)
{
MessageBox.Show("B");
}
else if (numberGrade >= 70 && numberGrade <= 80)
{
MessageBox.Show("C");
}
else if (numberGrade >= 60 && numberGrade <= 70)
{
MessageBox.Show("D");
}
else if (numberGrade <= 70)
{
MessageBox.Show("F");
}
}
It also said
Test your algorithm by running the program, entering a value in the textbox on the screen, and clicking on the "Convert To Letter Grade" button.
What should I do?
Edit: Sorry I needed to be more clear I have a number I must enter into a box (discard the magic number thing)
and I am getting the ERROR:AlgorithmTest.convertToLetterGrade(int): Not all paths return a value
.
Upvotes: 3
Views: 5501
Reputation: 1
Try and modify your code by adding a + or - sign to the grade like c+ or B-.
Upvotes: -1
Reputation: 32770
First of all, convertToLetterGrade
needs to return a char
because thats the condition you've imposed yourself choosing the method' signature. So ok, let's return a char
:
private char convertToLetterGrade(int numberGrade)
{
// replace with student code(Which I have done)
if (numberGrade >= 90 && numberGrade <= 100)
{
return `A`;
}
else if (numberGrade >= 80 && numberGrade <= 90)
{
return `B`;
}
else if (numberGrade >= 70 && numberGrade <= 80)
{
return `C`;
}
else if (numberGrade >= 60 && numberGrade <= 70)
{
return `D`;
}
else if (numberGrade <= 60)
{
return `F`;
}
}
But this is still wrong. Do you see why? Mentally work out the path execution would follow if numberGrade
were to be 110
? Does this method return anything in that case?
The specification of your method states that any value that doesn't belong to [100, 60)
must be an F
so, if you follow this precisely, your method should be:
private char convertToLetterGrade(int numberGrade)
{
// replace with student code(Which I have done)
if (numberGrade >= 90 && numberGrade <= 100)
{
return `A`;
}
else if (numberGrade >= 80 && numberGrade <= 90)
{
return `B`;
}
else if (numberGrade >= 70 && numberGrade <= 80)
{
return `C`;
}
else if (numberGrade >= 60 && numberGrade <= 70)
{
return `D`;
}
else
{
return `F`;
}
}
Do you see the difference? Now, any numberGrade
will follow a path that returns something. Having convertToLetterGrade(110)
return F
seems rather weird, but hey, thats the specification you were given!
But we can still do better. Notice that if else
clauses are evaluated in order, so you can optimize this somewhat doing the following:
private char convertToLetterGrade(int numberGrade)
{
// replace with student code(Which I have done)
if (numberGrade > 100)
{
return `F`
}
if (numberGrade >= 90)
{
return `A`;
}
else if (numberGrade >= 80)
{
return `B`;
}
else if (numberGrade >= 70)
{
return `C`;
}
else if (numberGrade >= 60)
{
return `D`;
}
else
{
return `F`;
}
}
There is no need to check the upper limit in each else if
clause because if you've already checked it in the previous one; avoid unnecessary checks.
And last (and this is primarily opinion) based, normally you should tend to avoid multiple returns in a method if possible. You will see many books implement similar methods in the following way:
private char convertToLetterGrade(int numberGrade)
{
// replace with student code(Which I have done)
char grade;
if (numberGrade > 100)
{
grade = `F`;
}
if (numberGrade >= 90)
{
grade = `A`;
}
else if (numberGrade >= 80)
{
grade = `B`;
}
else if (numberGrade >= 70)
{
grade = `C`;
}
else if (numberGrade >= 60)
{
grade = `D`;
}
else
{
grade = `F`;
}
return grade;
}
Upvotes: 0
Reputation: 13146
AlgorithmTest.convertToLetterGrade(int): Not all paths return a value
The method should return a value for every case;
private char convertToLetterGrade(int numberGrade)
{
// replace with student code(Which I have done)
char letter = 'F';
if (numberGrade >= 90 && numberGrade <= 100)
{
MessageBox.Show("A");
letter = 'A'
}
else if (numberGrade >= 80 && numberGrade < 90)
{
MessageBox.Show("B");
letter = 'B'
}
else if (numberGrade >= 70 && numberGrade < 80)
{
MessageBox.Show("C");
letter = 'C'
}
else if (numberGrade >= 60 && numberGrade < 70)
{
MessageBox.Show("D");
letter = 'D'
}
return letter;
}
Also, be careful with usage of <=
and <
.
Upvotes: 2
Reputation: 166
You are getting the error because your function is
private CHAR ConvertToLetterGrade(int numberGrade)
but you are not returning a char in your function. If it is not necessary to return a value change it to
private void ConvertToLetterGrade(int numberGrade)
EDIT: Since all you are doing is showing a message box then you do not need to return a value. If your function does not need to return a value you should set it to type void. You were very close with your first set of code but just need a few changes.
private void convertToLetterGrade(int numberGrade)
{
// replace with student code(Which I have done)
if (numberGrade >= 90 && numberGrade <= 100)
{
MessageBox.Show("A");
}
else if (numberGrade >= 80 && numberGrade < 90)
{
MessageBox.Show("B");
}
else if (numberGrade >= 70 && numberGrade < 80)
{
MessageBox.Show("C");
}
else if (numberGrade >= 60 && numberGrade < 70)
{
MessageBox.Show("D");
}
else
{
MessageBox.Show("F");
}
}
Also make sure that you are comparing the correct things in your if statements. Originally you had numberGrade >= 80 && numberGrade <= 90
for letter grade 'B'. This will return true if the number grade is 90 because of the <=
(less than or equal to) which is not correct since a 'B' is between 80-89.
Upvotes: 0
Reputation: 4537
There are two ways to solve your question. If you don't need to return value (only show) then just change char
type of the function to void
. Otherwise you have to add return
operation for each case, for example:
if (numberGrade >= 90 && numberGrade <= 100
{
MessageBox.Show("A");
return 'A';
}
Also the function should return some default value regardless numberGrade
value. It is possible by adding return 'O'
after all blocks of conditions.
Upvotes: 0