如何使我的扫描仪在多种方法中工作

huangapple 未分类评论52阅读模式
标题翻译

How to get my scanner work in more than one method

问题

我正在为学校制作一个四连珠游戏。我有一个玩家类定义了每个玩家的属性,一个棋盘类,一个“逻辑”类以及一个运行游戏的游戏类。我的玩家类有两种方法,几乎相同,用于设置每个玩家,唯一的区别是游戏棋子是X还是O。玩家方法还会读取注册ID和游戏标识。它在玩家1上运行得很好,但是在尝试运行使用in.next...()的任何方法时都会出现java.util.NoSuchElementException错误。

玩家类方法

public void setPlayer1() {
    Scanner in = new Scanner(System.in);
    System.out.println("\n玩家1,请输入您的Arena注册ID。");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("玩家1,请输入您想要使用的名称。");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " 您的游戏棋子是 \"X\"。");
    this.setPiece("X");
    in.close();
}

public void setPlayer2() {
    Scanner in = new Scanner(System.in);
    System.out.println("\n玩家2,请输入您的Arena注册ID。");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("玩家2,请输入您想要使用的名称。");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " 您的游戏棋子是 \"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);// 切换玩家
        int column = Connect4.askForColumn(turn);
    }
}

注意:上述代码仅为您提供的内容的翻译,其中的变量、类名和方法名可能需要根据您的实际代码进行调整。

英文翻译

I am making a connect 4 game for school. I have a player class defining aspects of each player, Board class, "logic" class and game class which runs the game. My Player class has two methods almost identacle to set up each player only difference is the game piece, X or O. The Player method also reads in a registration ID and gamerTag. It runs flawlessly on player1, but I get java.util.NoSuchElementException. I actually get this error when trying to run any method that uses in.next...();

Player Class Methods

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

答案1

得分: 0

你只需要将它作为参数传递给方法

public void setPlayer1(Scanner in) {
    System.out.println("玩家1,请输入您的竞技场注册ID。");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("玩家1,请输入您想要使用的名称。");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " 您的游戏棋子是 \"X\"。");
    this.setPiece("X");
}

public void setPlayer2(Scanner in) {
    System.out.println("玩家2,请输入您的竞技场注册ID。");
    this.setID(in.nextLong());
    System.out.print("");
    System.out.println("玩家2,请输入您想要使用的名称。");
    this.setTag(in.next());
    System.out.print("");
    System.out.print(gamerTag + " 您的游戏棋子是 \"O\"。");
    this.setPiece("O");
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Connect4TextConsole game = new Connect4TextConsole();
    System.out.print(board);
    // 这里你将 Scanner 传递给了你的方法
    player1.setPlayer1(in);
    player2.setPlayer2(in);
    in.close();
    winner = Connect4.checkForWin(board);
    while (!winner) {
        turn = turn.playerTurn(player1, player2); // 切换玩家
        int column = Connect4.askForColumn(turn);
    }
}
英文翻译

You just need to give it to the method as an argument

public void setPlayer1(Scanner 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");
    
}

public void setPlayer2(Scanner 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");
    }

`public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Connect4TextConsole game = new Connect4TextConsole();
    System.out.print(board);
//Here u give the scanner ur methods
    player1.setPlayer1(in);
    player2.setPlayer2(in);
    in.close();
    winner = Connect4.checkForWin(board);
    while (!winner){
        turn = turn.playerTurn(player1, player2);// switch players
        int column = Connect4.askForColumn(turn);

    }
}` 

huangapple
  • 本文由 发表于 2020年5月31日 02:13:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/62106798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定