我这里有两个代码块。一个扫描仪正确地等待用户输入,而另一个扫描仪直接吹过它并调用nextInt()
它返回一个NoSuchElementException
. 这是有效的块:
public void startGame() {
out.println("Player1: 1 for dumb player, 2 for smart player, 3 for human player.");
Scanner scan = new Scanner(System.in);
p = scan.nextInt();
if (p == 1)
p1 = new DumbPlayer("ONE");
if (p == 2)
p1 = new SmartPlayer("ONE");
else
p1 = new HumanPlayer("ONE");
out.println("Player2: 1 for dumb player, 2 for smart player, 3 for human player.");
p = scan.nextInt();
if (p == 1)
p2 = new DumbPlayer("TWO");
if (p == 2)
p2 = new SmartPlayer("TWO");
else
p2 = new HumanPlayer("TWO");
scan.close();
这是没有的块:
public int findBestMove(Set<Integer> moves, Board b) {
Set<Integer> set = new HashSet<Integer>();
out.println("Player " +name+ ", select a column from 1-7: ");
Scanner scan = new Scanner(System.in); <--here it should wait for input, but does not!
int move = scan.nextInt(); <-- NoSuchElementException
scan.close();
for (int x = 1; x <= 7; x++) {
set.add(move);
move += 7;
}
....etc
这两个都是独立的类,并且是从另一个类的主方法调用的。基本上main()
调用startGame()
,然后调用findBestMove()
一些 Player 类的方法......这是非工作代码所在的位置。程序中是否存在不适合接受输入的情况?我的印象是,只要我想要用户输入,我就可以使用这种方法。谢谢!