Java - 围绕 for 循环迭代以创建游戏玩家并设置他们的名字
Java - Iterating around for-loop to create players for game and setting their name
我编程的时间不长,我花了几个小时试图让这个方法做我需要的事情,所以在这一点上非常感谢帮助。我正在创建一个棋盘游戏,提示玩家输入玩家数量,然后依次提示他们输入他们的名字,然后将创建这些新玩家和他们的名字 set/stored。我还想确保两个玩家不能重名。
这是我播放器的一部分class:
public class Player {
private String name;
private int currentPosition;
private int distance;
private boolean quit;
public Player() {
}
public Player(String name, int currentPosition, int distance, boolean quit) {
this.name = name;
this.currentPosition = currentPosition;
this.distance = distance;
this.quit = quit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我有一个 setNumberOfPlayers 方法,它运行良好,并且在调用 getNumberOfPlayers() 时获得正确的玩家数量。
public class SetUpGame {
Scanner sc = new Scanner(System.in);
public void addPlayers() {
String playerName;
ArrayList<Player> players = new ArrayList<Player>();
System.out.println("What are the names of the " + this.numberOfPlayers + " players?\n");
for (int loop = 1; loop <= getNumberOfPlayers(); loop++) {
System.out.println("Player " + loop + " please enter your name");
boolean valid = false;
do {
playerName = sc.nextLine();
Player player = new Player();
for (Player play : players) {
if (players.isEmpty() || !play.getName().equalsIgnoreCase(playerName)) {
player.setName(playerName);
players.add(player);
System.out.println("success, your name is: " +playerName);
valid = true;
} else {
System.out.println("please enter another name");
valid = false;
}
}
} while (valid == false);
}
sc.close();
}
当从 main 调用添加播放器方法时,它给出以下输出:
What are the names of the 2 players?
Player 1 please enter your name
...并且用户可以输入他们的名字,但扫描仪永远不会关闭或循环询问下一个玩家的名字。我把代码搞得一团糟,以至于此时我自己都感到困惑。任何人都可以帮助解决这个问题,以及如何 validate/check 创建玩家并设置他们的名字?谢谢
您的内部 for 循环正在遍历 players 数组,但它是空的。
也许试试这个:
do {
System.out.println("Please enter name:");
playerName = sc.nextLine();
if(playerName.length()!=0){
valid = true;
for(Player play : players)
if(play.getName().equalsIgnoreCase(playerName))
valid = false;
if(valid){
Player player = new Player();
player.setName(playerName);
players.add(player);
}
}
} while (!valid);
我认为您可能很难调试,因为您所有的变量名都非常相似。 ('players','player','play')
编辑:已更新以检查是否重复
您在播放器中遗漏了一些 getter 和 setter class。
Player.java
public class Player {
private String name;
private int currentPosition;
private double distance;
private boolean quit;
public Player() {
// no-args constructor
}
public Player(String name, int currentPosition, double distance, boolean quit) {
this.name = name;
this.currentPosition = currentPosition;
this.distance = distance;
this.quit = quit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public boolean isQuit() {
return quit;
}
public void setQuit(boolean quit) {
this.quit = quit;
}
}
您可以按如下方式实现 SetUpGame class。
无需创建 do-while 循环。
SetUpGame.java
import java.util.ArrayList;
import java.util.Scanner;
public class SetUpGame {
Scanner sc = new Scanner(System.in);
ArrayList<Player> players = new ArrayList<Player>();
int numberOfPlayers;
public int getNumberOfPlayers() {
return numberOfPlayers;
}
public void setNumberOfPlayers(int numberOfPlayers) {
this.numberOfPlayers = numberOfPlayers;
}
public void addPlayers() {
System.out.print("Enter number of players: ");
numberOfPlayers = sc.nextInt();
System.out.println("What are the names of the " + numberOfPlayers + " players?");
for(int loop = 1; loop <= numberOfPlayers; loop++) {
Player player = new Player();
System.out.print("Enter name for player " + loop + ": ");
player.setName(sc.next());
System.out.print("Enter current position for player " + loop + ": ");
player.setCurrentPosition(sc.nextInt());
System.out.print("Enter distance for player " + loop + ": ");
player.setDistance(sc.nextDouble());
System.out.print("Enter validity for player " + loop + ": ");
player.setQuit(sc.nextBoolean());
players.add(player);
}
sc.close();
}
public void displayAllPlayers() {
System.out.println("Displaying all players...");
for(Player p : players) {
System.out.printf("%s %d %f %b", p.getName(), p.getCurrentPosition(), p.getDistance(), p.isQuit());
}
}
}
您终于可以测试您的 SetUpGame class。
测试SetUpGame.java
public class TestSetUpGame {
public static void main(String[] args) {
SetUpGame game = new SetUpGame();
game.addPlayers();
game.displayAllPlayers();
}
}
首先,球员名单在方法addPlayers
中被定义为一个局部变量,所以当这个方法完成时,所有球员数据都丢失了。因此有必要修改此方法以将玩家添加到 SetupGame
class 字段或将 return 玩家列表添加到调用方法。后一种方式是一种更简洁的填充播放器列表的功能方式。
接下来,为了高效检测重名,建议创建并使用Set<String> playerNames
。
最后,如果 Scanner
在 System.in
上打开,则不能关闭它,因为之后无法输入任何用户输入。
也就是说,addPlayers
的代码可能如下所示:
public List<Player> addPlayers() {
List<Player> players = new ArrayList<>();
Set<String> playerNames = new HashSet<>();
Scanner sc = new Scanner(System.in);
System.out.println("What are the names of the " + this.numberOfPlayers + " players?\n");
for (int loop = 1; loop <= this.numberOfPlayers; loop++) {
while (true) {
System.out.println("Player " + loop + " please enter your name");
String playerName = sc.nextLine();
if (playerNames.add(playerName)) {// Set.add returns true if name was added to it
Player p = new Player();
p.setName(playerName);
players.add(p);
break; // input new player
}
}
}
return players;
}
我编程的时间不长,我花了几个小时试图让这个方法做我需要的事情,所以在这一点上非常感谢帮助。我正在创建一个棋盘游戏,提示玩家输入玩家数量,然后依次提示他们输入他们的名字,然后将创建这些新玩家和他们的名字 set/stored。我还想确保两个玩家不能重名。
这是我播放器的一部分class:
public class Player {
private String name;
private int currentPosition;
private int distance;
private boolean quit;
public Player() {
}
public Player(String name, int currentPosition, int distance, boolean quit) {
this.name = name;
this.currentPosition = currentPosition;
this.distance = distance;
this.quit = quit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我有一个 setNumberOfPlayers 方法,它运行良好,并且在调用 getNumberOfPlayers() 时获得正确的玩家数量。
public class SetUpGame {
Scanner sc = new Scanner(System.in);
public void addPlayers() {
String playerName;
ArrayList<Player> players = new ArrayList<Player>();
System.out.println("What are the names of the " + this.numberOfPlayers + " players?\n");
for (int loop = 1; loop <= getNumberOfPlayers(); loop++) {
System.out.println("Player " + loop + " please enter your name");
boolean valid = false;
do {
playerName = sc.nextLine();
Player player = new Player();
for (Player play : players) {
if (players.isEmpty() || !play.getName().equalsIgnoreCase(playerName)) {
player.setName(playerName);
players.add(player);
System.out.println("success, your name is: " +playerName);
valid = true;
} else {
System.out.println("please enter another name");
valid = false;
}
}
} while (valid == false);
}
sc.close();
}
当从 main 调用添加播放器方法时,它给出以下输出:
What are the names of the 2 players?
Player 1 please enter your name
...并且用户可以输入他们的名字,但扫描仪永远不会关闭或循环询问下一个玩家的名字。我把代码搞得一团糟,以至于此时我自己都感到困惑。任何人都可以帮助解决这个问题,以及如何 validate/check 创建玩家并设置他们的名字?谢谢
您的内部 for 循环正在遍历 players 数组,但它是空的。
也许试试这个:
do {
System.out.println("Please enter name:");
playerName = sc.nextLine();
if(playerName.length()!=0){
valid = true;
for(Player play : players)
if(play.getName().equalsIgnoreCase(playerName))
valid = false;
if(valid){
Player player = new Player();
player.setName(playerName);
players.add(player);
}
}
} while (!valid);
我认为您可能很难调试,因为您所有的变量名都非常相似。 ('players','player','play')
编辑:已更新以检查是否重复
您在播放器中遗漏了一些 getter 和 setter class。
Player.java
public class Player {
private String name;
private int currentPosition;
private double distance;
private boolean quit;
public Player() {
// no-args constructor
}
public Player(String name, int currentPosition, double distance, boolean quit) {
this.name = name;
this.currentPosition = currentPosition;
this.distance = distance;
this.quit = quit;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCurrentPosition() {
return currentPosition;
}
public void setCurrentPosition(int currentPosition) {
this.currentPosition = currentPosition;
}
public double getDistance() {
return distance;
}
public void setDistance(double distance) {
this.distance = distance;
}
public boolean isQuit() {
return quit;
}
public void setQuit(boolean quit) {
this.quit = quit;
}
}
您可以按如下方式实现 SetUpGame class。
无需创建 do-while 循环。
SetUpGame.java
import java.util.ArrayList;
import java.util.Scanner;
public class SetUpGame {
Scanner sc = new Scanner(System.in);
ArrayList<Player> players = new ArrayList<Player>();
int numberOfPlayers;
public int getNumberOfPlayers() {
return numberOfPlayers;
}
public void setNumberOfPlayers(int numberOfPlayers) {
this.numberOfPlayers = numberOfPlayers;
}
public void addPlayers() {
System.out.print("Enter number of players: ");
numberOfPlayers = sc.nextInt();
System.out.println("What are the names of the " + numberOfPlayers + " players?");
for(int loop = 1; loop <= numberOfPlayers; loop++) {
Player player = new Player();
System.out.print("Enter name for player " + loop + ": ");
player.setName(sc.next());
System.out.print("Enter current position for player " + loop + ": ");
player.setCurrentPosition(sc.nextInt());
System.out.print("Enter distance for player " + loop + ": ");
player.setDistance(sc.nextDouble());
System.out.print("Enter validity for player " + loop + ": ");
player.setQuit(sc.nextBoolean());
players.add(player);
}
sc.close();
}
public void displayAllPlayers() {
System.out.println("Displaying all players...");
for(Player p : players) {
System.out.printf("%s %d %f %b", p.getName(), p.getCurrentPosition(), p.getDistance(), p.isQuit());
}
}
}
您终于可以测试您的 SetUpGame class。
测试SetUpGame.java
public class TestSetUpGame {
public static void main(String[] args) {
SetUpGame game = new SetUpGame();
game.addPlayers();
game.displayAllPlayers();
}
}
首先,球员名单在方法addPlayers
中被定义为一个局部变量,所以当这个方法完成时,所有球员数据都丢失了。因此有必要修改此方法以将玩家添加到 SetupGame
class 字段或将 return 玩家列表添加到调用方法。后一种方式是一种更简洁的填充播放器列表的功能方式。
接下来,为了高效检测重名,建议创建并使用Set<String> playerNames
。
最后,如果 Scanner
在 System.in
上打开,则不能关闭它,因为之后无法输入任何用户输入。
也就是说,addPlayers
的代码可能如下所示:
public List<Player> addPlayers() {
List<Player> players = new ArrayList<>();
Set<String> playerNames = new HashSet<>();
Scanner sc = new Scanner(System.in);
System.out.println("What are the names of the " + this.numberOfPlayers + " players?\n");
for (int loop = 1; loop <= this.numberOfPlayers; loop++) {
while (true) {
System.out.println("Player " + loop + " please enter your name");
String playerName = sc.nextLine();
if (playerNames.add(playerName)) {// Set.add returns true if name was added to it
Player p = new Player();
p.setName(playerName);
players.add(p);
break; // input new player
}
}
}
return players;
}