簡體   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