Java ||在while循环中未按预期工作

Java ||在while循环中未按预期工作,java,while-loop,comparator,Java,While Loop,Comparator,我有一个我似乎无法解决的问题。我的目标是提示用户玩游戏的人数。我希望在用户的输入满足以下要求时继续提示用户: -输入不是整数 -输入少于三个 -输入大于7 发生的情况是,似乎需要3个输入来检查第三个条件,一个用于第一个条件,两个用于第二个条件。它并不是一次检查所有内容,我认为这与语法.nextInt()有关,但我找不到其他方法来表达这一点。提前感谢您的帮助 代码如下: public void setNumPlayers(Game game) { Scanner input = new S

我有一个我似乎无法解决的问题。我的目标是提示用户玩游戏的人数。我希望在用户的输入满足以下要求时继续提示用户: -输入不是整数 -输入少于三个 -输入大于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();
}
编辑: 我已经更改了代码以将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();
}
public void setNumPlayers(游戏){
扫描仪输入=新扫描仪(System.in);
有多少人在玩;
而(!input.hasNextInt()){
System.out.println(“请输入播放人数”);
input.next();
}
int x=input.nextInt();
while(x7){
System.out.println(“玩家数量必须至少为三人,且不得超过七人。”);
x=input.nextInt();
}
游戏。setNumPlayers(x);
input.close();
}
通过调用Scanner.nextInt()两次,您可以读取两个数字,而不是相同的数字两次


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

使用&&operator。也可以使用局部变量并从scanner中指定值。在条件允许的情况下使用它

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();
 }
intx=0;
if(input.hasNextInt())
{
x=input.nextInt();
}
而(x>3&&x<7){
System.out.println(“请输入玩游戏的人数。你必须至少有三名玩家,但不超过七名。”);
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();
有多少人在玩?
请输入播放的人数。你必须至少有三名球员,但不得超过七名。
10
请输入播放的人数。你必须至少有三名球员,但不得超过七名。
托托
请输入播放的人数。你必须至少有三名球员,但不得超过七名。
图图
请输入播放的人数。你必须至少有三名球员,但不得超过七名。
22
请输入播放的人数。你必须至少有三名球员,但不得超过七名。
6.
扫描仪输入=新扫描仪(System.in);
有多少人在玩;
int numPlayers=0;
而(人数<3 | |人数>7){
System.out.println(“请输入玩家数量。您必须至少有三名玩家,且不超过七名。”);
如果(!input.hasNextInt()){
input.next();
}
否则{
numPlayers=input.nextInt();
}
}
游戏。setNumPlayers(numPlayers);
input.close();

nextInt()赋值给变量。当您两次调用
nextInt()
时,您会得到不同的结果。非常感谢!我有一个很长的代码块可以工作,但是如果代码太长,我总是觉得很奇怪,好像它一定是错的。这一定是个骗局。你的回答很好,谢谢。
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();