public void createPlayer() {
for (int i = 0; i < 3; i++) { // 这里可以设置创建的人数
System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名:");
while (true) {
try {
boolean flag = true;
while (flag) {
System.out.println("请输入第" + (i + 1) + "位玩家的ID:");
input = new Scanner(System.in);
ID = input.nextInt();
if (playersList.size() == 0) {
flag = false;
break;
}
for (Player player:playersList) {
if (ID == player.getPlayerId()) {
System.out.println("ID重复,请重新输入");
flag = true;
break;
} else
flag = false;
}
}
System.out.println("输入姓名:");
String name = input.next();
playersList.add(new Player(ID, name));
break;
} catch (InputMismatchException e) {
System.out.println("请输入整数类型的ID!");
}
}
}
for (Player player1 : playersList) {
System.out.println("-----欢迎玩家:" + player1.getPlayerName());
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章