手记

好多简易扑克手记中,都没有输入相同ID后报错并重新输入,写了段很笨的代码(可以分段封装一下)

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

}


}


1人推荐
随时随地看视频
慕课网APP