I started coding small program in Java. I wanted to exercise try-catch block, but i did not even come to that part and got stuck on loop part. I know this is very basic loop issue, but i guess i caught myself in a very simple logical problem. What I need from this program is if a user press 1, then to jump to switch statement and execute proper case. If a user press anything but 1 or 2, to go back to the MenuLoop function and execute it again until pressed correct number (1 or 2). I used While loop for control. Here is the code.
import java.util.Scanner;
public class TryCatchExercise {
public static void MenuLoop() {
Scanner input = new Scanner(System.in);
int choice;
System.out.println("1. Check for Number 1");
System.out.println("2. Check for Number 2");
System.out.print("Please enter your choice... ");
choice = input.nextInt();
while (choice != 1 || choice != 2) {
System.out.println("Invalid entry, press 1 or 2");
MenuLoop();
} //Isn't it logical at this point for loop to be skipped and
// go to Switch if a user pressed 1 or 2.??
switch (choice) {
case 1:
System.out.println("Pressed 1");
break;
case 2:
System.out.println("Pressed 2");
break;
default:
System.out.println("Invalid number");
}
}
public static void main(String[] args) {
MenuLoop();
}
}
OUTPUT
1. Check for Number 1
2. Check for Number 2
Please enter your choice... 1
Invalid entry, press 1 or 2
1. Check for Number 1
2. Check for Number 2
Please enter your choice... 2
Invalid entry, press 1 or 2
1. Check for Number 1
2. Check for Number 2
Please enter your choice... 5
Invalid entry, press 1 or 2
1. Check for Number 1
2. Check for Number 2
Please enter your choice...
You need a logical and (not or) here
while (choice != 1 || choice != 2) {
System.out.println("Invalid entry, press 1 or 2");
MenuLoop();
}
should be something like
while (choice != 1 && choice != 2) {
System.out.println("Invalid entry, press 1 or 2");
MenuLoop();
}
or (using De Morgan's laws ) like
while (!(choice == 1 || choice == 2)) {
System.out.println("Invalid entry, press 1 or 2");
MenuLoop();
}
Part of the problem is that you are recursively calling menuLoop
from within menuLoop
If you had a while loop within main
then you could just do a return if the proper keys is not pressed.
so main would be something like
while (!menuLoop () {
System.out.println("Invalid entry, press 1 or 2");
}
and menuLoop
would return a boolean
public static boolean MenuLoop() {
....
System.out.println("1. Check for Number 1");
System.out.println("2. Check for Number 2");
System.out.print("Please enter your choice... ");
choice = input.nextInt();
if(choice != 1 && choice != 2) { // and NOT or
return false;
}
switch (choice) {
case 1:
System.out.println("Pressed 1");
break;
case 2:
System.out.println("Pressed 2");
break;
default:
System.out.println("Invalid number");
}
return true;
Also please remember that the scanner.nextInt
will not swallow up the Enter
that may or may not be pressed.
Maybe you can try the following code. In your code , it's not need to use iteration.
choice = input.nextInt();
while (choice != 1 && choice != 2) {
System.out.println("Invalid entry, press 1 or 2");
choice = input.nextInt();
}
This is a logical problem, the "choice" value should be either 1 or 2. In your (while) statement you are checking that "choice" is not different from 1 but also that "choice" is not different from 2. This condition is never reached because "choice" can be either 1 or 2 but not both values at the same time. This is only an explanation of the situation.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.