gpt4 book ai didi

java - Java中不同类之间传递数组元素

转载 作者:行者123 更新时间:2023-11-30 04:20:11 34 4
gpt4 key购买 nike

我无法将保存名称和数字的数组中的元素传递到另一个将进行计算的类。这就是我为类(class)准备的内容。

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 的销售额并将其与数组中其他卖家的销售额进行比较。从那里我试图展示其他人需要多多少销售额才能达到等于或超过最畅销产品的水平。这就是我遇到的问题。我不知道如何将该数组中的值传递到下一个类,以便我可以进行计算。有什么想法吗?

最佳答案

您可以将其设为 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());
}

}

关于java - Java中不同类之间传递数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17243669/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com