請(qǐng)問(wèn)下面的代碼,為什么不會(huì)提示id重復(fù)?跪求解答
public void createPlayer() ?{
System.out.println("------------------創(chuàng)建玩家---------------------");
Integer id;
String name;
int i=0;
for(int j=0;j<2;j++) {
while(true) {
try{
System.out.println("請(qǐng)輸入第"+(i+1)+"個(gè)玩家的id:");
id=dllx.nextInt();
if(player.contains(new Players(id,null))){
System.out.println("你輸入的id已被使用!請(qǐng)重新輸入!");
continue;
}
break;
} catch (Exception ?e) {
System.out.println("你的輸入有誤,請(qǐng)重新輸入!");
dllx=new Scanner(System.in);
continue;
}
}
while(true){
System.out.println("請(qǐng)輸入第"+(i+1)+"個(gè)玩家的姓名:");
name =dllx.next();
if(player.contains(new Players(id,name))) {
System.out.println("你輸入的名字已被使用!請(qǐng)重新輸入!");
continue;
}
break;
}
player.add( new Players(id,name));
i++;
}
}
2018-06-13
簡(jiǎn)單看了一下,還沒(méi)調(diào)試,在分別判斷名字或者id的時(shí)候,比較一個(gè)對(duì)象就可以了, 你方法中好像是傳入了2個(gè)對(duì)象。。。
if(player.contains(new Players(id,null))){
System.out.println("你輸入的id已被使用!請(qǐng)重新輸入!");
continue;
你仔細(xì)梳理下這個(gè)邏輯。。。
2018-06-13
你說(shuō)不會(huì)提示用戶名相同的話,那就是 contains()方法有問(wèn)題吧。 你仔細(xì)調(diào)試下。。。