[英]Issue with logic and Loop in Java
I started coding small program in Java. 我开始用Java编写小程序。 I wanted to exercise try-catch block, but i did not even come to that part and got stuck on loop part.
我想练习try-catch块,但是我什至没有来到那部分而陷入循环部分。 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.
我需要从该程序中获得帮助,如果用户按1,则跳转到switch语句并执行适当的大小写。 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).
如果用户按1或2以外的任何键,则返回MenuLoop功能并再次执行,直到按正确的数字(1或2)为止。 I used While loop for control.
我使用While循环进行控制。 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
问题的一部分是,你是递归调用
menuLoop
从内menuLoop
If you had a while loop within main
then you could just do a return if the proper keys is not pressed. 如果您在
main
内部有一个while循环,那么如果没有按正确的键,您可以返回。
so main would be something like 所以主要是这样的
while (!menuLoop () {
System.out.println("Invalid entry, press 1 or 2");
}
and menuLoop
would return a boolean 并且
menuLoop
将返回一个布尔值
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. 另外请记住,
scanner.nextInt
不会吞下可能会或可能不会按下的Enter
。
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. 这是一个逻辑问题,“ choice”值应为1或2。在您的(while)语句中,您正在检查“ choice”与1相同,而且“ choice”与2相同。这种情况永远不会达到,因为“选择”可以是1或2,但不能同时是两个值。 This is only an explanation of the situation.
这只是对情况的一种解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.