noel
noel

Reputation: 2349

Ridiculous use of a switch statement

I was doing some some Java homework with my friend. The instructor wants him to write a Java program to translate an integer input into a numeric grade. 100-90 = A and so on. The only catch is that he must use a switch statement. He must also:

Include a case in your switch statement that will display a polite error message if the user enters any number less than 0 or greater than 100."

Originally I thought of this...

import java.util.Scanner;
public class grade
{
    public static void main(String[] args)
    {
        int ng;//number grade
        String lg = "";//letter grade

        System.out.println("enter grade");

        Scanner in = new Scanner(System.in);

        ng = in.nextInt();

        switch (ng/10) 
        {
            case 10: 
            case 9:     
                lg = "A";  
                break;
            case 8:
                lg = "B";  
            break;
            case 7:     
                lg = "C";  
            break;
            case 6:     
                lg = "D";  
            break;
            default:     
                lg = "F";  
            break;
        }   
         System.out.println("You got an " + lg);
      }
}

This isn't perfect because it allows values over 100 and below 0, but I am trying to avoid typing out every integer from 100-0. This seems like a ridiculous use of a switch statement and I can't imagine why a college professor would teach it, other than to illustrate the DRY principle.

Is there a better way that still uses the switch statement, but doesn't type every int from 100-0?

Upvotes: 0

Views: 2357

Answers (4)

Joachim Isaksson
Joachim Isaksson

Reputation: 180987

You could always add some minor complexity to your switch expression to make the cases simpler; this will calculate 90-100 as 10, 80-89 as 9 and so on, 101 and above will become 11 and above, and every input below 0 will become 0 or negative so they'll fall under default;

switch ((ng-ng/100+10)/10)
{
    case 10:
        lg = "A";
        break;
    case 9:
        lg = "B";
        break;
    case 8:
        lg = "C";
        break;
    case 7:
        lg = "D";
        break;
    case 6: case 5: case 4: 
    case 3: case 2: case 1:
        lg = "F";
        break;
    default:
        System.out.println("Polite Error");
        lg = "";
}

Upvotes: 2

Brian Roach
Brian Roach

Reputation: 76908

How about:

String lg = null;
switch (ng/10) 
{
    case 10:
        if (ng > 100) {
            // polite error
            break;
        }
    case 9:
        lg = "A"
        break;
    case 8:
        lg = "B";  
        break;
    case 7:     
        lg = "C";  
        break;
    case 6:
        lg = "D";
        break;
    default:
         if (ng < 0) {
             // polite error
             break;
         }
         lg = "F";
         break;
}

After the switch you'd have to check if a grade were set or not.

if (lg == null) {
    System.out.println("The input score was > 100 or < 0");
} else {
    System.out.println("You got an " + lg);
}

Upvotes: 0

Cody A. Ray
Cody A. Ray

Reputation: 6017

Nice use of integer division ;)

Okay, well this code officially embarrasses me and makes me cry. But here, just using switch statements.

import java.util.Scanner;
public class grade
{
    public static void main(String[] args)
    {
        int ng;//number grade
        String lg = "";//letter grade
        boolean error = false;

        System.out.println("enter grade");

        Scanner in = new Scanner(System.in);

        ng = in.nextInt();

        switch (ng/10) 
        {
            case 10: 
                switch (ng)
                {
                     case 100:
                         lg = "A";
                         break;
                     default:
                         error = false;
                         break;
                }
                break;
            case 9:     
                lg = "A";  
                break;
            case 8:
                lg = "B";  
            break;
            case 7:     
                lg = "C";  
            break;
            case 6:     
                lg = "D";  
            break;
            case 5:     
            case 4:     
            case 3:     
            case 2:     
            case 1:     
            case 0:     
                lg = "F";  
                break;
            default:     
                error = true;
            break;
        }   

         if (error) {
           System.out.println("Sorry, the grade must be between 0 and 100");
         } else {
           System.out.println("You got an " + lg);
         }
      }
}

Blech.

Upvotes: 0

Halogen
Halogen

Reputation: 541

Yeah, no way in hell you would want to actually use a switch statement for this lol. But the way you've suggested is about the best way I can think of to do it.

I would make the default be for the error scenario though, because that could any integer less than 0 or over 100. Between 0 and 100, at least you have a finite number of cases (though you'll have to repeat the "F" case several times).

Upvotes: 0

Related Questions