//創(chuàng)建玩家
public?void?creatPlayer(){
????System.out.println("-----------開始創(chuàng)建玩家-------------");
????Player?newplayer?=?new?Player();
????for(int?i=?1?;i<3?;?i++)?{
????????System.out.println("請輸入第"?+?i?+?"位玩家的ID和姓名");
????????while?(true)?{
????????????try?{
????????????????System.out.println("輸入ID");
????????????????int?id?=?console.nextInt();
????????????????for?(Player?player?:?playerList)?{
????????????????????if?(player.getId()==(id))?{
????????????????????????System.out.println("該ID已被占用,請重新輸入");
????????????????????????throw?new?Exception();
????????????????????}
????????????????}
????????????????newplayer.setId(id);
????????????????break;
????????????}?catch?(InputMismatchException?e)?{
????????????????console.next();
????????????????System.out.println("輸入錯誤,請重新輸入");
????????????}?catch?(Exception?e)?{
????????????????continue;
????????????}
????????}
????????System.out.println("請輸入姓名");
????????String?name?=?console.next();
????????newplayer.setName(name);
????????playerList.add(newplayer);
????????//i++;
????}
????for(int?j?=0?;?j<playerList.size();j++){
????????System.out.println("歡迎玩家"?+?playerList.get(j).getName());
????}
}
2019-08-04
出現(xiàn)覆蓋 說明你的List創(chuàng)建的位置不對 查看一下是否是List隨著你的for循環(huán)而重新實例化了
2019-08-07
你要new兩次player,你new一次就只有個一個對象,你一直在給同一個對象賦值當(dāng)然會覆蓋