Hum*_*ey4 2 java arrays boolean
我正在尝试检查数组中是否已使用名称,但是它仅适用于现货[0]。我假设它从布尔中的for循环仅经历一次并且不递增以检查其他点?
试图改变不同的if和while循环
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果将名字命名为“ Warehouse”是可行的,而我尝试第二次使用“ Warehouse”。但是,如果我尝试输入与第二个插槽相同的名称,它就不会恢复为真实。
您需要return false;
在for循环中删除for循环(如果放在该循环中),只需使用索引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;
}
Run Code Online (Sandbox Code Playgroud)
您只能像这样没有short变量来做短。
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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
120 次 |
最近记录: |