java文件中获取员工工资最高的人的代码
时间: 2023-06-09 20:08:39 浏览: 74
下面是一个例子:
```
public class Employee {
private String name;
private int salary;
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public int getSalary() {
return salary;
}
}
public class Main {
public static void main(String[] args) {
Employee[] employees = new Employee[3];
employees[0] = new Employee("Alice", 52000);
employees[1] = new Employee("Bob", 65000);
employees[2] = new Employee("Charlie", 48000);
int highestSalary = 0;
String employeeWithHighestSalary = "";
for (Employee employee : employees) {
if (employee.getSalary() > highestSalary) {
highestSalary = employee.getSalary();
employeeWithHighestSalary = employee.getName();
}
}
System.out.println("The employee with the highest salary is: " + employeeWithHighestSalary);
}
}
```
这个例子中,我们有一个 Employee 类来表示每个员工的名字和工资。在 Main 类中,我们创建了一个包含所有员工的数组。然后,我们使用一个 for 循环来找出工资最高的员工。最后,我们打印出该员工的名字。
阅读全文