Humeey4:
私は名前がすでにアレイで使用されているかどうかを確認しようとしているが、それが唯一のスポット[0]のために働いています。私は仮定していてからbooleanのループを一度だけ通過しておよび他のスポットをチェックするまで増加していませんか?
以下の場合と、ループしながら、さまざまに変化しようとしました
if (depotCount < 4){ // Runs if the depots are less than 4
System.out.println("Enter your depots name");
name = console.next().toLowerCase();
if (check(depots, name) == true){
System.out.println("Depot name already exists");
break;
}
else {
addDepot(depots, name);
}
} else {
System.out.println("Only 4 depots are allowed");
break;
}
public boolean check(Depot [] depots, String name){
boolean check = false;
for (int i = 0; i < depots.length; i++){
if(depots[i].getDepotName().equals(name))
return true;
else {
return false;
}
}
return check;
}
「倉庫」と私のように最初の名前は「倉庫」二度目を入れしようとした場合、それが働いているので。私は第二のスロットと同じ名前を入れしようとした場合、それは本当のように戻ってきていません。
飛燕グエン:
あなたは削除する必要がreturn false;
あなたがそこに置く場合は、インデックス0で一度だけ実行するforループ、ループのために。
public boolean check(Depot [] depots, String name){
boolean check = false;
for (int i = 0; i < depots.length; i++){
if(depots[i].getDepotName().equals(name))
return true;
}
return check;
}
あなただけのショートチェック変数なしでこれを好きなことができます。
public boolean check(Depot [] depots, String name){
for (int i = 0; i < depots.length; i++){
if(depots[i].getDepotName().equals(name))
return true;
}
return false;
}