【发布时间】:2021-08-03 02:36:57
【问题描述】:
在一个学校项目中,我需要制作一个具有以下选项的菜单系统:
- 加载员工数据 - 提示用户输入要加载的员工数量,然后提示输入每个员工的姓名、ID(5 位数字)和年薪
- 添加新员工 - 提示用户输入员工数据、姓名、身份证和年薪
- 显示所有员工 - 将每个员工的数据显示到控制台,每行一名员工
- 检索特定员工的数据 - 提示用户输入员工 ID 并显示相应的员工数据:ID、姓名和薪水
- 根据范围检索具有工资的员工 - 提示用户输入最低和最高工资,并显示所有工资在该范围内的员工。在单独的行上显示每个员工的所有信息 - 姓名、ID 和薪水
- 退出
每个菜单选项都必须是它自己的方法,他更喜欢我们使用数组而不是列表,这就是我分配 100 个空格的原因。这是我到目前为止所拥有的。菜单选项 1、2 和 6 在不中断程序的情况下运行。但是任何必须展示的东西似乎都坏了。我最好的猜测是在所有方法之间传递和更新数组“员工”存在问题。
package practice;
import java.util.Arrays;
import java.util.Scanner;
public class Project {
public static Employee[] loadData() {
Scanner scanint = new Scanner(System.in);
Scanner scanstr = new Scanner(System.in);
System.out.println("How many employees would you like to add?"); //Determines how many employees to create
Employee[] employees = new Employee[100];
int numberEmployees = scanint.nextInt();
for (int i = 0; i < numberEmployees; i++) {
System.out.println("Enter name: ");
String empName = scanstr.nextLine();
System.out.println("Enter ID: ");
int empID = scanint.nextInt();
System.out.println("Enter Salary: ");
int empSalary = scanint.nextInt();
employees[i+1] = new Employee(empName, empID, empSalary); //Add employee to array
}
return employees;
}
public static Employee addEmployee() {
Scanner scanint = new Scanner(System.in);
Scanner scanstr = new Scanner(System.in);
System.out.println("Enter name: ");
String empName = scanstr.nextLine();
System.out.println("Enter ID: ");
int empID = scanint.nextInt();
System.out.println("Enter salary: ");
int empSalary = scanint.nextInt();
return new Employee(empName, empID, empSalary);
}
public static void displayEmployees(Employee[] employees) {
for (int i = 0; i < employees.length; i++) {
if (employees[i] != null) {
System.out.println(Arrays.toString(employees));
//System.out.println("Employee Name: " + employees[i].name + " ID: " + employees[i].id + " Salary: " + employees[i].salary);
}
}
}
public static void specificEmployee(Employee[] employees, int id) {
for (int i = 0; i < employees.length; i++) {
if (employees[i].id == id) {
System.out.println("Name: " + employees[i].name + " ID: " + employees[i].id + " Salary: " + employees[i].salary);
}
else {
System.out.println("ID not recognized");
}
}
}
public static void salaryRange(Employee[] employees, int salaryMinimum, int salaryMaximum) {
for (int i = 0; i < employees.length; i++) {
if (employees[i].salary >= salaryMinimum && employees[i].salary <= salaryMaximum) {
System.out.println("Name: " + employees[i].name + " ID: " + employees[i].id + " Salary: " + employees[i].salary);
}
else {
System.out.println("No employees within salary range");
}
}
}
public static void main(String[] args) {
Scanner scanint = new Scanner(System.in);
Scanner scanstr = new Scanner(System.in);
int menuChoice = 0;
while (menuChoice != 6) {
System.out.println("\tMenu:");
System.out.println("1. Load Employee Data");
System.out.println("2. Add New Employee");
System.out.println("3. Display All Employees");
System.out.println("4. Retrieve Specific Employee Data");
System.out.println("5. Retrieve Employees Within Salary Range");
System.out.println("6. Exit");
menuChoice = scanint.nextInt();
Employee[] employees = new Employee[100];
int amountEmployees;
if (menuChoice == 1) {
employees = loadData();
}
else if (menuChoice == 2) {
employees[0] = addEmployee();
}
else if (menuChoice == 3) {
displayEmployees(employees);
}
else if (menuChoice == 4) {
System.out.println("Enter 5 digit employee ID: ");
int id = scanint.nextInt();
specificEmployee(employees, id);
}
else if (menuChoice == 5) {
System.out.println("Enter minimum of salary range: ");
int salaryMinimum = scanint.nextInt();
System.out.println("Enter maximum of salary range");
int salaryMaximum = scanint.nextInt();
salaryRange(employees, salaryMinimum, salaryMaximum);
}
else if (menuChoice == 6) {
break;
}
else {
System.out.println("Invalid Choice");
}
}
scanint.close();
scanstr.close();
}
任何帮助将不胜感激!!!!!!
【问题讨论】:
-
您可能应该为每个数组包含一个整数,该整数定义了所用元素的数量。此外,除非您将每个元素都设置为 null,否则这些元素不能保证为 null。
-
您的第二个菜单项仅设置数组的元素 0。您不能添加两个或更多员工。我建议花一些时间学习如何使用 ide 的调试器。
标签: java arrays object methods