||在while循环不按预期工作

时间:2015-03-02 11:27:13

标签: java while-loop comparator

我遇到了一个似乎无法修复的问题。我的目标是提示用户玩游戏的人数。我想在输入满足以下要求时继续提示用户: -input不是整数 - 输入少于三个 - 输入大于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();
}

3 个答案:

答案 0 :(得分:2)

通过两次调用Scanner.nextInt(),您可以读取两个数字,而不是两次相同的数字。

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

答案 1 :(得分:2)

如果你想检查输入并继续要求输入直到输入正常,你必须修改一点逻辑。以下内容适用于以下输入序列:

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();

答案 2 :(得分:1)

使用&amp;&amp;运营商。并使用局部变量并从扫描仪分配值。并在条件下使用它。

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();
 }