mercredi 1 avril 2020

Java using return value from method for if else statements

I made a method in java that prints a menu screen that looks like this:

MENU
c - Number of whitespace characters
f - Find text
r - Replace all !'s
q - Quit

Choose an option:

The method returns a char. How do I use the return value of the method in main to make if else statements?

printMenu method:

public static char printMenu(Scanner scnr) {
      char menuOp;

      //display the menu
      System.out.println("\nMENU");
      System.out.println( "c - Number of whitespace characters");
      System.out.println("f - Find text");
      System.out.println("r - Replace all !\'s");
      System.out.println("q - Quit\n");

      menuOp = ' ';

      //loop until the user has entered a c, f, r or q
      while (menuOp != 'c' && menuOp != 'f' &&
                menuOp != 'r' &&  
                menuOp != 'q') {
         System.out.println( "Choose an option:");
         menuOp = scnr.nextLine().charAt(0);
      }

      //return the letter that the user entered
      return menuOp;
   }  //end of the printMenu method

What I want to be able to do in main:

while (return value from printMenu method != 'q'){

      printMenu(scnr);
      if (return value from printMenu method == 'c'){ //do this                    
      }
      else if (return value from printMenu method == 'f'){ //do this
      }
      else if (return value from printMenu method == 'r'){ //do this
      }
   }
}

I'm still new and really appreciate the help, patience, and kindness. Thanks

Edit - I have to use the return value from printMenu() as a requirement for a project.

Aucun commentaire:

Enregistrer un commentaire