[英]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.