Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I use a switch() statement to convert from a numeric to a letter grade?
    primarykey
    data
    text
    <p>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)</p> <p>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.</p> <p>Here is my functional class:</p> <pre><code>public class Grade { public double getGrade(int input) { double inputGrade; switch(input) { case 1: inputGrade &gt;= 90; break; case 2: inputGrade &gt;= 80; break; case 3: inputGrade &gt;= 70; break; default: grade = 60; } return grade; } } </code></pre> <p>Here is my test class:</p> <pre><code>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); } } </code></pre> <p>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.</p> <p>I know this is incomplete, but this is as far as I could go without making errors.</p> <p>EDIT:WOW! Thanks guys, i hate to pick a single correct answer, because a lot of you helped me!</p> <p>This is what i ended up with (that worked :D)</p> <pre><code>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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload