public void createPlayer() throws Exception {
System.out.println("------------------创建玩家---------------------");
int i=0;
while(i<2) {
System.out.println("请输入第"+(i+1)+"个玩家的id:");
int id =dllx.nextInt();
Players p=player.get(id);
if(p==null) {
System.out.println("请输入名字:");
String name =dllx.next();
Players newplayer= new Players(id,name);
player.put(id, newplayer);
i++;
} else {
System.out.println("你输入的id已被占用,请重新输入!");
continue;
}
}
System.out.println("***欢迎玩家***");
Set<Integer> key= player.keySet();
for (Integer integer : key) {
Players p = player.get(integer);
if(p!=null) {
System.out.println("玩家:"+p.getName());
}
}
}
不知道你想抛出书名异常,视频讲解的抛出异常是id的类型不匹配的时候抛出的,你可以在接受id的时候使用try catch 语句进行抛出异常啊。你的代码里没见你生成异常,到是已经抛出异常了~