我的作业应该提示用户带有5个选项的消息框:1 - 添加员工信息(员工姓名,职位和薪水)2 - 编辑员工信息3 - 计算工资4-打印全部员工,头衔,工资5 - 退出计划。我让程序运行正常,但是当用户输入员工的姓名,职位和薪水时,它不会将其保存到数组中。你能帮助我看看是什么情况吗?再次感谢。
public class Track_Employee_Information {
public static void main(String[] args) {
String[] EMPLOYEES = new String [15];
String[] TITLE = new String [15];
double[] SALARY = new double[15];
int total_emp = 0;
double total_salary = 0;
int user_input;
//Main includes user's responses to accept the right input.
do {
user_input = getUserInput(total_emp);
if (user_input == 1) {
getNewEmp(EMPLOYEES, TITLE, SALARY, total_emp);
}
if (user_input == 2) {
getChangeEmp(user_input, EMPLOYEES, TITLE, SALARY);
}
if (user_input == 3) {
getSalaryCost(total_emp, total_salary, SALARY);
}
if (user_input == 4) {
getPrintEmployees(total_emp, EMPLOYEES, TITLE, SALARY);
}
} while (user_input != 5);
if (user_input == 5) {
System.exit(0);
}
}
private static void getPrintEmployees(int total_emp, String[] EMPLOYEES, String[] TITLE, double[] SALARY) {
//Display employees, titles, and salaries. If there are no employees, return error message.
if (total_emp == 0) {
JOptionPane.showMessageDialog(null, "There are no employees or information to display");
}
else {
JOptionPane.showMessageDialog(null, EMPLOYEES + "\n" + TITLE + "\n" + SALARY);
}
}
private static double getSalaryCost(int total_emp, double total_salary, double[] SALARY) {
//Calculate total salary costs and display. If there are no employees, return error message.
if (total_emp == 0) {
JOptionPane.showMessageDialog(null, "There are no employees to calculate the salary from");
}
for (int i = 0; i < SALARY.length; i++) {
total_salary += SALARY[i];
}
return total_salary;
}
private static void getChangeEmp(int user_input, String[] EMPLOYEES, String[] TITLE, double[] SALARY) {
//Allow user to input name of pre-existing employee in order to change name, title, and salary. If employee does not exist, return error message.
String askUserPrevEmp;
askUserPrevEmp = JOptionPane.showInputDialog("Please Enter the pre-existing employee name");
for (int i = 0; i < EMPLOYEES.length; i++) {
try {
while (askUserPrevEmp != EMPLOYEES[i]) {
JOptionPane.showMessageDialog(null, "This Employee does not exist.");
askUserPrevEmp = JOptionPane.showInputDialog("Please Enter the pre-existing employee name");
}
if (askUserPrevEmp.equals(EMPLOYEES[i])) {
EMPLOYEES[i] = JOptionPane.showInputDialog("Please enter the new Employee Name");
TITLE[i] = JOptionPane.showInputDialog("Please enter the new Employee title");
SALARY[i] = Double.parseDouble(JOptionPane.showInputDialog("Please enter the new Employee salary"));
}
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Error: Please enter a number not a character");
}
while ((SALARY[i] < 10000) || (SALARY[i] > 500000)) {
JOptionPane.showMessageDialog(null, "Error: Please enter a NUMERIC value between $10,000 - $500,000");
SALARY[i] = Double.parseDouble(JOptionPane.showInputDialog("Please enter the new Employee salary"));
}
break;
}
}
private static int getNewEmp(String[] EMPLOYEES, String[] TITLE, double[] SALARY, int total_emp) {
//Allow user to enter new employee, title, salary. It will be validated as a numeric number and between $10,000-$50,000
String emp_name = "";
String emp_title = "";
double emp_salary= 0;
try {
emp_name = JOptionPane.showInputDialog("Please enter the employee name:");
for (int i = 0; i < EMPLOYEES.length; i++) {
EMPLOYEES[i] = emp_name;
}
emp_title = JOptionPane.showInputDialog("Please enter the employee title");
for (int j = 0; j < TITLE.length; j++) {
TITLE[j] = emp_title;
}
emp_salary = Double.parseDouble(JOptionPane.showInputDialog("Please enter the employee salary"));
for (int k = 0; k < SALARY.length; k++) {
while ((emp_salary < 10000) || (emp_salary > 500000)) {
JOptionPane.showMessageDialog(null, "Error: Please enter a numeric value between $10,000 - $500,000");
emp_salary = Double.parseDouble(JOptionPane.showInputDialog("Please enter the employee salary"));
}
SALARY[k] = emp_salary;
}
total_emp++;
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Error: Please enter a number not a character");
emp_salary = Double.parseDouble(JOptionPane.showInputDialog("Please enter the employee salary"));
}
return total_emp;
}
private static int getUserInput(int total_emp) {
//Presenting the user with a menu to select options. If there are more than 15 employees, return error message. If not then accept response.
String askUserInput;
int user_input = 0;
askUserInput = "1) Add a new employee \n2) Change an employee \n3) Calculate salary costs \n4) Print employees \n5) Exit the program";
user_input = Integer.parseInt(JOptionPane.showInputDialog(null, askUserInput));
if ((user_input == 1) && (total_emp > 15)) {
JOptionPane.showMessageDialog(null, "You have already reached the maximum number of 15 employees");
}
while ((user_input<1) || (user_input>5)) {
JOptionPane.showMessageDialog(null, "Error: Please enter a number between 1-5");
user_input = Integer.parseInt(JOptionPane.showInputDialog(askUserInput));
}
return user_input;
}
}
答案 0 :(得分:0)
对不起文字墙。
这些值存储在getNewEmp方法的数组中。问题是total_emp的值永远不会改变,因此,您的系统会抱怨没有员工要显示。
您似乎在该方法中递增后返回它,因此您可以通过更改对此的调用来修复它:
total_emp = getNewEmp(EMPLOYEES, TITLE, SALARY, total_emp);
每次调用getNewEmp函数时,该方法中的循环也会覆盖数组的内容。您只需要将数据写入数组中的相应索引,就像这样。
EMPLOYEES[total_emp] = emp_name;
TITLE[total_emp] = emp_title;
SALARY[total_emp] = emp_salary;
此外,getPrintEmployees将无法按照您期望的方式运行。 EMPLOYEES,TITLE和SALARY是数组,并试图通过将它们放入print语句来打印它们只会给你一个内存地址或其他东西。您需要遍历每个条目:
String details = "";
for (int i = 0; i < total_emp; i++)
{
details += EMPLOYEES[i] + "\n" + TITLE[i] + "\n" + SALARY[i] + "\n\n";
}
JOptionPane.showMessageDialog(null, details);
答案 1 :(得分:0)
你的值被添加但是以错误的方式,不要使用循环,否则它会用输入中的值填充整个数组,这就是它的作用。使用ArrayList,只需执行.add(emp_name),。add(emp_title)等操作。使用循环添加值将覆盖所有值,并使用用户输入的当前值。
您也不需要将数组或任何变量传递给任何方法。可能会发生什么事,但我不完全确定我会再看一下。