||在 while 循环中没有按预期工作

|| in while loop not working as expected

我遇到了似乎无法解决的问题。我的目标是提示用户玩游戏的人数。我想在用户输入满足以下要求时继续提示用户: - 输入不是整数 -输入小于三 -输入大于 7

发生的事情是似乎需要 3 个输入来检查第三个条件,一个用于第一个,两个用于第二个。它不是一次检查所有这些,我认为它与语法 .nextInt() 有关,但我找不到另一种表达方式。提前感谢您的帮助!

代码如下:

public void setNumPlayers(Game game) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");
    while(!input.hasNextInt() || input.nextInt() < 3 || input.nextInt() > 7) {
        System.out.println("Please eneter the number of people playing. You must have at least three players, and no more than seven.");
        input.next();
    }
    game.setNumPlayers(input.nextInt());
    input.close();
}

和主要的调用

Game g = new Game();
    ConsoleOutput io = new ConsoleOutput();
    io.setNumPlayers(g);
    System.out.println(g.getNumPlayers());

编辑: 我更改了代码以将 nextInt 存储为变量。下面的代码有效,如果我输入字母会提示我,如果我输入一个超出其参数的数字,会提示我并让我重新分配 x,唯一的问题是如果我输入不正确的数字,然后是一个字母,它会崩溃......我应该将其封装在某种 try/catch 中吗?这似乎不切实际。

public void setNumPlayers(Game game) {
    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");     

    while(!input.hasNextInt()) {            
        System.out.println("Please eneter the number of people playing.");
        input.next();
    }
    int x = input.nextInt();
    while(x<3 || x>7) {
        System.out.println("The number of players must be at least three and no more than seven.");
        x = input.nextInt();              
    }

    game.setNumPlayers(x);
    input.close();
}

通过调用 Scanner.nextInt() 两次,您读取了两个数字,而不是同一个数字两次。

考虑读取 int 值,将其存储在变量中并在您的 if 语句中使用它。

使用 && 运算符。并且还使用局部变量并从扫描仪分配值。并在条件下使用它。

int x =0;
 if(input.hasNextInt())
 {
 x= input.nextInt();
 }
 while( x > 3 && x < 7) {
    System.out.println("Please eneter the number of people playing. You must have at least three players, and no more than seven.");
   input.next();
 }

你必须稍微修改一下逻辑,如果你想检查输入并继续要求输入直到输入正确。以下将适用于一系列输入,例如:

How many people are playing?
Please enter the number of people playing. You must have at least three players, and no more than seven.
10
Please enter the number of people playing. You must have at least three players, and no more than seven.
toto
Please enter the number of people playing. You must have at least three players, and no more than seven.
tutu
Please enter the number of people playing. You must have at least three players, and no more than seven.
22
Please enter the number of people playing. You must have at least three players, and no more than seven.
6


    Scanner input = new Scanner(System.in);
    System.out.println("How many people are playing?");

    int numPlayers = 0;
    while (numPlayers < 3 || numPlayers > 7) {
        System.out.println("Please enter the number of people playing. You must have at least three players, and no more than seven.");
        if (!input.hasNextInt()) {
            input.next();
        }
        else {
            numPlayers = input.nextInt();
        }
    }
    game.setNumPlayers(numPlayers);
    input.close();