This IS NOT homework, but it is a practice that the teacher gave us to help study for a test. But i will not be turning this in to the teacher. (i'm hoping that is allowed)
He wants a user to input a grade and have it assigned a letter grade. It would be easy with if statements, but i can't use ANY! I have to use the switch method.
Here is my functional class:
public class Grade {
public double getGrade(int input)
{
double inputGrade;
switch(input)
{
case 1: inputGrade >= 90;
break;
case 2: inputGrade >= 80;
break;
case 3: inputGrade >= 70;
break;
default:
grade = 60;
}
return grade;
}
}
Here is my test class:
import java.util.*;
public class TestGrade
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int input = scan.nextInt();
Grade lGrade = new Grade();
double finalGrade = lGrade.getGradeSwitch(input);
System.out.println("Your toll is $" + finalGrade);
}
}
I just haven't been programming enough to have this analytical mind. I HAVE tried to complete it, i just haven't found a way to covert the user input (int) into a letter grade (string) without if statements.
I know this is incomplete, but this is as far as I could go without making errors.
EDIT:WOW! Thanks guys, i hate to pick a single correct answer, because a lot of you helped me!
This is what i ended up with (that worked :D)
public String getGrade(int input)
{
String letterGrade;
switch(input/10)
{
case 9: letterGrade = "A";
break;
case 8: letterGrade = "B";
break;
case 7: letterGrade = "C";
break;
case 6: letterGrade = "D";
default:
letterGrade = "F";
}
return letterGrade;
}