我无法将包含名称和数字的数组中的元素传递给另一个将进行计算的类。这就是我对课程的要求。
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的销售额并将它们与阵列中其他卖家的销售额进行比较。在那里,我试图展示其他人需要多少销售额才能达到或超过畅销书。这是我遇到的问题。我不知道如何将这个数组中的值传递给下一个类,所以我可以进行计算。有什么想法吗?
答案 0 :(得分:2)
不是在静态方法中创建数组,而是可以使它成为EmployeeArray
的成员,然后可以在静态上下文中重用该数组(不推荐)或创建不同的EmployeeArray
来保存不同的员工阵列,这在OOP中更有意义。粗略草图:
import java.util.Scanner;
public class EmployeeArray {
private Employee[] employees;
public Employee[] getEmployees() {
return employees;
}
public void setUpArray() {
Scanner input = new Scanner(System.in);
int numEmp;
System.out.println("Enter how may employees to be compared: ");
numEmp = input.nextInt();
input.nextLine();
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;
}
}
public int getMaxSellerEmployeeSales() {
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();
}
public static void main(String[] args) {
EmployeeArray google = new EmployeeArray();
google.setUpArray();
EmployeeArray ms = new EmployeeArray();
ms.setUpArray();
EmployeeArrayCalculatorAndManipulator eGoogle = new EmployeeArrayCalculatorAndManipulator();
EmployeeArrayCalculatorAndManipulator eMS = new EmployeeArrayCalculatorAndManipulator();
eGoogle.doSomethingWithArray(google.getEmployees());
eMS.doSomethingWithArray(ms.getEmployees());
}
}
答案 1 :(得分:0)
对我来说很有用: -
包EmployeePackage;
公共类员工{
private int sales;
private String name;
public Employee(String employeeName, int employeeSales){
this.name = employeeName;
this.sales = employeeSales;
}
public String getName(){
return this.name;
}
public int getSales(){
return this.sales;
}
}
包EmployeePackage;
import java.util.Scanner; 公共类EmployeeArray {
public static void main(String[] args) {
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();
employees[i] = new Employee(employeeName , employeeSales); //calling constructor
}
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());
}
}