繁体   English   中英

|| 在while循环中无法按预期工作

[英]|| in while loop not working as expected

我有一个似乎无法解决的问题。 我的目标是提示用户玩游戏的人数。 我要在用户输入满足以下要求时继续提示用户:-输入不是整数-输入小于3-输入大于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,唯一的问题是如果我输入了错误的数字,然后输入字母,则会崩溃...我应该将其封装在某种尝试/捕获中吗? 那似乎不切实际。

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语句中使用它。

如果要检查输入并继续要求输入直到输入良好,则必须修改一点逻辑。 以下将适用于一系列输入,例如:

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

使用&&运算符。 并使用局部变量并从扫描仪分配值。 并适当使用它。

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

暂无
暂无

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

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