我在将数组中包含名称和数字的元素传递给另一个将进行计算的类时遇到问题。这是我对班上的东西。在Java中的不同类之间传递数组元素
public class Employee {
private int sales;
private String name;
public Employee(String name, int sales){
this.name = name;
this.sales = sales;
}
public String getName(){
return this.name;
}
public int getSales(){
return this.sales;
}
}
该类将帮助存储数组的名称和销售额。这是它的独立类中的数组。
import java.util.Scanner;
public class EmployeeArray {
public static int employee() {
Scanner input = new Scanner(System.in);
int numEmp;
System.out.println("Enter how may employees to be compared: ");
numEmp = input.nextInt();
input.nextLine();
Employee[] employees = new Employee[numEmp];
for (int i = 0; i < numEmp; i++) {
System.out.print("Enter your name: ");
String employeeName = input.nextLine();
System.out.println();
System.out.print("Enter your annual sales: ");
int employeeSales = input.nextInt();
input.nextLine();
System.out.println();
Employee employee = new Employee(employeeName, employeeSales);
employees[i] = employee;
}
Employee maxSeller = employees[0];
for (int i = 0; i < employees.length; i++) {
System.out.println("Employee Name: " + employees[i].getName());
System.out.println("Total sales: $" + employees[i].getSales());
System.out.println();
if (maxSeller.getSales() < employees[i].getSales()) {
maxSeller = employees[i];
}
}
System.out.println();
System.out.println("Top Seller is: " + maxSeller.getName());
System.out.println("Top Sales are: $" + maxSeller.getSales());
return maxSeller.getSales();
}
}
数组中存放的值,并返回最畅销称为maxSeller并得到姓名和销售额他们。现在我遇到的问题是,在一个单独的类中,我需要获取存储在此数组中的值来执行一些计算。我必须得到maxSeller的销售额,并将它们与阵列中其他卖家的销售额进行比较。在那里,我正在试图展示其他人需要达到的平均销售额或超过最高销售额的销售额。这是我遇到的问题。我不知道如何将数组中的值传递给下一个类,以便我可以进行计算。有什么想法吗?
什么是单独的类?您应该将您的员工数组作为参数传递给您创建的任何其他类中的方法。从那里,你可以用干将访问销售或名称 – sunrize920
类似这样的问题经常建议在程序上设计的解决方案不能被轻易采用面向对象的范例的情况。如果你要停止写入解决这个就像你正在编写一个BASIC程序,并开始考虑参与模型中的对象和行为,你想那些对象来实现,如“我如何通过程序之间阵列”问题成为更少的问题。 – scottb
单独的类是我使用最畅销的产品的销售,并应用了一个委员会,寻找当年的总薪酬,那么就比较其他销售人员的报酬总额,并告诉他们,他们需要多少更多的是为了配合顶部卖卖方的赔偿。 –