java - the best option to build one menu, switch-case or if-if-if? -
I want to create a menu that will close the program only when the user chooses to exit option. I want to know your opinion about this option and why.
or sequential Your second method will not even compile. You can not declare a function within a method in Java. Besides, you can infinitely detect loops because you do not update the options inside the loop. Your first method is better than for various reasons. Switch Statement:
do {menu (); Switch (Option) {Case 1: Method 1 (); break; Case 2: Method 2 (); break; }} While (option! = 3); Println ("Bye!"); Return;
if details in a recursive method:
scanner input = new scanner ( System.in); Int option = input.nextInt (); Menu (); Public Zero Menu () {if (option == 1) {method1 (); } If (option == 2) {method2 (); } If (option == 3) {System.out.println ("bye!"); Return; } Menu (); }
Comments
Post a Comment