英文:
comparison logic in this if statement
问题
以下是代码部分的翻译:
public class Workplace {
private int index = 0;
private String name;
private Employee[] employees;
public Workplace(String name, int numberOfEmployees){
this.name = name;
this.employees = new Employee[numberOfEmployees];
}
public Employee[] returnEmployees(Employee e){
Employee youngestEmployee = null;
Employee oldestEmployee = null;
for(int i=0; i<index; i++){
if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
youngestEmployee = employees[i];
}
else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
oldestEmployee = employees[i];
}
}
return new Employee[] {youngestEmployee, oldestEmployee};
}
}
特别是在这个部分的 if 语句比较逻辑:
if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
youngestEmployee = employees[i];
}
else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
oldestEmployee = employees[i];
}
我有另一个名为 Employee 的类,在该类中我声明了获取和设置方法。并且声明了 age 变量为 int 型。
所以我知道这个 if 语句的比较逻辑是想获取最年轻和最年长的员工,但是我在这部分感到困惑:
employees[i].getAge() > youngestEmployee.getAge()
与其说是小于,这里是大于。
我还没有找到答案。
谢谢你的时间!
英文:
I was wondering if you can explain to me this comparison logic:
public class Workplace {
private int index = 0;
private String name;
private Employee[] employees;
public Workplace(String name, int numberOfEmployees){
this.name = name;
this.employees = new Employee[numberOfEmployees];
}
public Employee[] returnEmployees(Employee e){
Employee youngestEmployee = null;
Employee oldestEmployee = null;
for(int i=0; i<index; i++){
if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
youngestEmployee = employees[i];
}
else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
oldestEmployee = employees[i];
}
}
return new Employee[] {youngestEmployee, oldestEmployee};
}
}
In particular, the if statement comparison logic over here:
if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
youngestEmployee = employees[i];
}
else if(oldestEmployee == null || employees[i].getAge() < oldestEmployee.getAge()){
oldestEmployee = employees[i];
}
I've got another class called Employee, in which I have declared the get and set methods. And have the age variable declared as an int.
So I know that this if statement comparison logic wants to get the youngest and oldest employee but I am getting confused at this part:
employees[i].getAge() > youngestEmployee.getAge()
Instead of saying less than, here it is greater than.
I couldn't find an answer yet.
Thank you for your time!
答案1
得分: 2
你对代码表示怀疑是正确的。比较看起来有问题。
if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
youngestEmployee = employees[i];
}
这段代码的意思是:如果当前员工的年龄大于最年轻员工的年龄,将当前员工设为最年轻员工。实际上应该相反。
英文:
You are right to doubt on the code. The comparison looks wrong.
if(youngestEmployee == null || employees[i].getAge() > youngestEmployee.getAge()){
youngestEmployee = employees[i];
}
This says: if the age of the current employee is greater than the youngest, set the current as the youngest.
It should be the contrary.
答案2
得分: 0
假设我们有以下情况:
this.employees = new Employee[5];
Employee youngestEmployee = null;
这里的逻辑是,它正在循环遍历所有员工的列表(目前有五名员工),并获取当前员工,即列表中在位置 i 处的员工(employees[i])。然后,使用 Employee 类中的一个 get 方法或 getter 来从您声明在顶部的 employees 数组类中获取该当前员工的年龄,并将其与先前员工(youngestEmployee)的年龄进行比较。最初,youngestEmployee 为 null,因此它将设置 youngestEmployee = employees[i](假设 employees[i] 的值为 25)。但在第二次比较时,它将比较位置 i 处的员工的年龄,并将其与先前值(即 25)进行比较。然后,根据从 if 语句中获得的结果,它将决定 youngestEmployee 的新值。
英文:
Lets suppose
this.employees = new Employee[5];
Employee youngestEmployee = null;
The logic here is that it's looping through a list of all employees(currently we have five employees) and takes the current employee meaning the employee at i(employees[i]) in that list of employees that you declared on the top and use a get method or a getter from your Employee class to get the age of that current employee from you employees array class and then compare it with the age of the previous employee(youngestEmployee). At first the youngestEmployee=null so it will set youngestEmployee=employees[i](suppose the value of employees[i]=25) but at the second time it will compare the age of the employee at i(employees[i]) and then compare it to the previous value(which was 25). Then it will decide the new value of youngestEmployee accoding to the result it got from the if statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论