如何以多种方法使我的扫描仪工作[重复]

问题描述 投票:0回答:1
我正在为学校制作Connect 4游戏。我有一个玩家类,定义每个玩家的各个方面,棋盘类,“逻辑”类和运行游戏的游戏类。我的Player类有两种几乎相同的方法来设置每个玩家,唯一的区别是游戏片段X或O。Player方法还读取注册ID和gamerTag。它可以在player1上完美运行,但是我得到了java.util.NoSuchElementException。当尝试运行任何使用in.next ...();]的方法时,我实际上收到了此错误

玩家类方法

public void setPlayer1() { Scanner in = new Scanner(System.in); System.out.println("\nPlayer 1 please enter your Arena registration ID."); this.setID(in.nextLong()); System.out.print(""); System.out.println("Player 1 please enter the name you would like to use."); this.setTag(in.next()); System.out.print(""); System.out.print(gamerTag + " your game piece is \"X\"."); this.setPiece("X"); in.close(); } public void setPlayer2() { Scanner in = new Scanner(System.in); System.out.println("\nPlayer 2 please enter your Arena registration ID."); this.setID(in.nextLong()); System.out.print(""); System.out.println("Player 2 please enter the name you would like to use."); this.setTag(in.next()); System.out.print(""); System.out.print(gamerTag + " your game piece is \"O\"."); this.setPiece("O"); in.close();} public static void main(String[] args) { Connect4TextConsole game = new Connect4TextConsole(); System.out.print(board); player1.setPlayer1(); player2.setPlayer2(); winner = Connect4.checkForWin(board); while (!winner){ turn = turn.playerTurn(player1, player2);// switch players int column = Connect4.askForColumn(turn); } }

我正在为学校制作Connect 4游戏。我有一个玩家类,定义每个玩家的各个方面,棋盘类,“逻辑”类和运行游戏的游戏类。我的Player类几乎有两种方法...
java java.util.scanner
1个回答
0
投票
您只需要将其作为参数提供给方法
© www.soinside.com 2019 - 2024. All rights reserved.