Java: do-while loop with switch statement

Language: Java

boolean x = false;
String letter = "NULL";

do{
    System.out.print("Enter grade (one character): ");
    letter = sc.next(); 
    switch (letter) {
       case "a": 
       x=true;
       break;
     case "A":
       x=true;
       break;      
     case "b":
       x=true;
       break;      
     case "B":
       x=true;
       break;      
     case "c":
       x=true;
       break;      
     case "C":
       x=true;
       break;      
     case "d":
       x=true;
       break;      
     case "D":
       x=true;
       break;
     case "f":
       x=true;
       break;      
     case "F":
       x=true;
       break;
     default:
       System.out.println("Invalid grade - must enter A,B,C,D,F (upper or lower case)");
       System.out.println(x);
       break;
     }
      System.out.println(x);
     }
      while(x=false);

      

-------------- output result ------------------------------- --- - output result --------------------------

Since I'm a beginner, I need a reputation of 10 to post snapshots of the output ... so here is the output by typing it ...

// this means if I enter String "e"

Please enter a grade (one character): e Invalid class - must enter A, B, C, D, F (upper or lower case)

falsely

falsely

// this means if I enter String "A"

Enter grade (one character): A

Invalid class - must enter A, B, C, D, F (upper or lower case)

True

Question ***: For my output, when I enter "e" I was hoping to see something like

Enter the grade (one character): e

Invalid class - must enter A, B, C, D, F (upper or lower case)

falsely

falsely

//goes through

Enter an estimate (one character):

So, I was hoping that when I enter e it will make it false ... and start from the beginning until the final result is entered: A, B, C, D, F (top or bottom case)

What's my mistake? I tried to brainstorm myself but I thought I would see what you guys have to say ....

If you think that the strategy I am using to get the result is not the best one ... What strategy / logic would you recommend?

Thanks for the help!

+3


source to share


3 answers


 while(x=false);

      



It is not right. (should be ==)

+2


source


Yes =

- the assignment operator you have to use ==

to check for equality and more, note that your switch can be shrunk down to the following and currently you have redundant code that is better used if the instruction from the ||

operator is here.



  switch (letter) {
     case "a": 
     case "A":    
     case "b":
     case "B":
     case "c":
     case "C":    
     case "d":   
     case "D":
     case "f":     
     case "F":
       x=true;
       break;
     default:
     //...

      

+1


source


You can simplify the switch statement even further. By converting a letter to uppercase, you check for both uppercase and lowercase. For example:

    do{
        System.out.print("Enter grade (one character): ");
        letter = sc.next(); 
        switch (letter.toUpperCase()) {
            case "A":                   
            case "B":       
            case "C":       
            case "D":       
            case "F":
                x=true;
                break;
            default:
                System.out.println("Invalid grade - must enter A,B,C,D,F (upper or lower case)");                    
                break;
        }                    
    } while(x==false);        

    System.out.println(x);

      

+1


source







All Articles