繁体   English   中英

如何检查数组是否与参数具有相同的值?

[英]how do I check if an array has the same value as the parameter?

public boolean admitHonorStudent(String firstName, String lastName,
                                          int question) {
      if(numStudent<=10){
          HS[numEmployee]=new honor(firstName,lastName, question);
          numStudent++;
          return true;
      }
      /*  for(int i = 1; i<HS.length; i++) {
          if(HS[i].getFirstname().equals(firstName) && HS[i].getLastname().equals(lastName)) {
              return false;
          }
      }*/
    return false;
}

上面的方法将一个CommissionedEmployee对象存储到CE数组中,我尝试使用for循环遍历数组并检查数组的i位置是否与参数相同。 因此,如果员工的名字和姓氏与数组中已有的名字相同,则返回 false。 但 for 循环似乎不起作用。

如果您的员工检查匹配,是否像返回true一样简单,即。

if (CE[i].getFirstname().equals(firstName) && CE[i].getLastname().equals(lastName)) {
   return true;  // change false to true
}

此外,您的for循环索引可能应该从0开始,即。

for (int i=0; i<CE.length; i++) { ... }

我会先形成一个对象,然后比较:

public boolean hireCommissionedEmployee(String firstName, String lastName,
                                          double commissionRate) {

          CommissionedEmployee object=new CommissionedEmployee(firstName,lastName, commissionRate);
          for(int i =0; i<CE.length; i++) {
             if(CE[i].equals(object) {
                return true;
          }
      }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM