2015-03-02 110 views
-1

我有一个问题,我似乎无法修复。我的目标是提示用户玩游戏的人数。我想继续提示用户,而他们的输入满足以下要求: - 输入不是整数 - 输入不足三人 - 输入比有七个||在while循环不按预期工作

较大的正在发生的事情是,它似乎需要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(); 
} 
+5

将'nextInt()'赋值给一个变量。当你两次调用nextInt()时,你会得到不同的结果。 – khelwood 2015-03-02 11:29:17

回答

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

非常感谢!我有一段非常长的代码,但是如果代码太长,我总觉得很奇怪,因为它一定是错误的。它应该是一个小菜。你的回答很好,谢谢。 – Qumbaala 2015-03-02 12:04:56

2

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

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

1

使用& &运营商。并使用本地变量并从扫描器中分配值。并在条件下使用它。

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

这不工作:你读了一个'int' _when_它是**不是**一个'int' ... – 2015-03-02 12:04:54

+0

@AbbéRésina谢谢指出它! – Prashant 2015-03-02 12:06:03