小编Hum*_*ey4的帖子

检查字符串是否存在于数组中

我正在尝试检查数组中是否已使用名称,但是它仅适用于现货[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)

java arrays boolean

2
推荐指数
1
解决办法
120
查看次数

标签 统计

arrays ×1

boolean ×1

java ×1