Answer the given question with a proper explanation and step-by-step solution. Java: please do not copy/ paste from another solution because it is wrong, I am looking for the same output /******* My question is how can I put the (while loop, do-while, if - else condition) in methods ? below: is a solution how can you fix it ? import java.util.Scanner; public class Main { public static void main(String[] args) { int menuChoice; Scanner keyboard = new Scanner(System.in); while (true) { do { printMenuOptions(); menuChoice = getUserChoice(keyboard); if (menuChoice <= -1) { System.out.println("Invalid number"); } else { if (menuChoice > 5) { System.out.println("Invalid number choose between 0 to 5."); } } } while (menuChoice <= -1 || menuChoice > 5 || menuChoice == 5); // Rest of your code goes here } } public static void printMenuOptions() { System.out.println("what do you like"); System.out.println("1. pepsi"); System.out.println("2. sprit"); System.out.println("3. tea"); System.out.println("4. coffie"); System.out.println("5. meal menu"); } public static int getUserChoice(Scanner keyboard) { System.out.print("Please choose: "); return keyboard.nextInt(); } }
Answer the given question with a proper explanation and step-by-step solution.
Java: please do not copy/ paste from another solution because it is wrong, I am looking for the same output /*******
My question is how can I put the (while loop, do-while, if - else condition) in methods ?
below: is a solution how can you fix it ?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int menuChoice;
Scanner keyboard = new Scanner(System.in);
while (true) {
do {
printMenuOptions();
menuChoice = getUserChoice(keyboard);
if (menuChoice <= -1) {
System.out.println("Invalid number");
} else {
if (menuChoice > 5) {
System.out.println("Invalid number choose between 0 to 5.");
}
}
} while (menuChoice <= -1 || menuChoice > 5 || menuChoice == 5);
// Rest of your code goes here
}
}
public static void printMenuOptions() {
System.out.println("what do you like");
System.out.println("1. pepsi");
System.out.println("2. sprit");
System.out.println("3. tea");
System.out.println("4. coffie");
System.out.println("5. meal menu");
}
public static int getUserChoice(Scanner keyboard) {
System.out.print("Please choose: ");
return keyboard.nextInt();
}
}
Step by step
Solved in 4 steps with 2 images