繁体   English   中英

Java中的逻辑和循环问题

[英]Issue with logic and Loop in Java

我开始用Java编写小程序。 我想练习try-catch块,但是我什至没有来到那部分而陷入循环部分。 我知道这是一个非常基本的循环问题,但是我想我陷入了一个非常简单的逻辑问题。 我需要从该程序中获得帮助,如果用户按1,则跳转到switch语句并执行适当的大小写。 如果用户按1或2以外的任何键,则返回MenuLoop功能并再次执行,直到按正确的数字(1或2)为止。 我使用While循环进行控制。 这是代码。

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... 

您需要一个逻辑和(不是或)

while (choice != 1 || choice != 2) {
    System.out.println("Invalid entry, press 1 or 2");
    MenuLoop();
} 

应该是这样的

while (choice != 1 && choice != 2) {
    System.out.println("Invalid entry, press 1 or 2");
    MenuLoop();
} 

(使用戴摩根定律

while (!(choice == 1 || choice == 2)) {
    System.out.println("Invalid entry, press 1 or 2");
    MenuLoop();
} 

问题的一部分是,你是递归调用menuLoop从内menuLoop

如果您在main内部有一个while循环,那么如果没有按正确的键,您可以返回。

所以主要是这样的

while (!menuLoop () {
  System.out.println("Invalid entry, press 1 or 2");
}

并且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;

另外请记住, scanner.nextInt不会吞下可能会或可能不会按下的Enter

也许您可以尝试以下代码。 在您的代码中,不需要使用迭代。

choice = input.nextInt();
while (choice != 1 && choice != 2) {
    System.out.println("Invalid entry, press 1 or 2");
    choice = input.nextInt();
}   

这是一个逻辑问题,“ choice”值应为1或2。在您的(while)语句中,您正在检查“ choice”与1相同,而且“ choice”与2相同。这种情况永远不会达到,因为“选择”可以是1或2,但不能同时是两个值。 这只是对情况的一种解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM