对于此计划,我正在尝试为输入的员工设置工资单。该程序让用户输入员工的姓名,税号和工资,并自动为输入的每个员工生成一个ID号。输入此信息后,要求用户提供2周的工作小时数。为此,输入员工ID号以引用工作一定小时数的员工。接下来,输入第1周或第2周以确定它们工作的周。最后,输入当周的工作小时数。信息完成后,用户输入0表示停止输入数据,工资单打印在图表中。我遇到的主要问题是,您必须输入每个员工添加到列表中的ID,周和小时,无论他们是否在2周内工作。如果我没有为某个员工输入任何信息,我想尝试弄清楚如何将工作小时数和总工资数设置为0。例如,如果我输入:1,对于员工1; 1,第1周; 20,在第1周工作数小时;按回车; 1,再次为员工1; 2,第2周; 30,在第2周工作的小时数,这些信息将打印在表格中。假设我有另一名员工,员工2.如果他们在第1周或第2周根本不工作,我不想为员工2输入所有这些信息,因此表格应该打印0表示工作小时数,0表示总薪水如果我决定不输入ID,周数和工作小时数。相反,它给了我一个outOfBoundsException:索引:1大小:1当我尝试这样做时出错。我想知道是否有人有解决方案。我的代码的3个类别发布在下面。在主类中给出了带有outOfBoundsException的已执行程序的示例。
import java.util.Scanner;
import java.util.ArrayList;
/**
* A class that contains methods for prompting a user for ACME employee information including names, tax IDs, and wages.
*/
public class EmployeeRecord
{
// ArrayLists used in methods.
ArrayList<String> employees = new ArrayList<String>();
ArrayList<String> tIds = new ArrayList<String>();
ArrayList<Double> wages = new ArrayList<Double>();
ArrayList<String> employeesLast = new ArrayList<String>();
Scanner in = new Scanner(System.in);
// Private Instance variables used in methods.
private String employeeId = "%03d";
private String employeeName = " ";
private String employeeNameLast = " ";
private String taxId = " ";
private double wage = 0.0;
/**
* A method that prompts the user to enter ACME employee names, tax IDs, and wages. This method also generates an ID number for each employee.
*/
public void setEmployeeInfo()
{
System.out.println("Please enter the names of each ACME employee, each employee tax ID, and each employee wage rate. Press Q when you are done entering names.");
while(!employeeName.equalsIgnoreCase("Q"))
{
employeeName = in.next();
if(employeeName.equalsIgnoreCase("Q"))
{
break;
}
employeeNameLast = in.next();
taxId = in.next();
wage = in.nextDouble();
employees.add(employeeName);
employeesLast.add(employeeNameLast);
tIds.add(taxId);
wages.add(wage);
System.out.println("Employee ID | Employee Name | Tax ID | Wage");
for(int i = 1; i <= employees.size(); i++)
{
System.out.printf(String.format(employeeId, i) + " | " + employees.get(i - 1) + " " + employeesLast.get(i - 1) + " | " + tIds.get(i - 1) + " | " + "%1.2f",wages.get(i - 1));
System.out.println();
}
}
}
/**
* A method that gets the list of ACME employee first names added to the record.
* @return
* Returns the ArrayList containing the first names of each employee entered.
*/
public ArrayList<String> getEmployeeArrayList()
{
return employees;
}
/**
* A method that gets the list of ACME employee last names added to the record.
* @return
* Returns the ArrayList containing the last names of each employee entered.
*/
public ArrayList<String> getEmployeeLastArrayList()
{
return employeesLast;
}
/**
* A method that gets the list of ACME employee tax IDs added to the record.
* @return
* Returns the ArrayList containing the tax IDs of each tax ID entered.
*/
public ArrayList<String> getTaxIdsArrayList()
{
return tIds;
}
/**
* A method that gets the list of ACME employee wages added to the record.
* @return
* Returns the ArrayList containing the wages of each wage entered.
*/
public ArrayList<Double> getWageArrayList()
{
return wages;
}
}
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/**
* A class that contains methods for entering payroll information for each ACME employee. This class uses methods from the EmployeeRecord class in order to run and therefore, relies on the EmployeeRecord class to operate.
*/
public class Employee
{
// ArrayLists that will implement return methods from the EmployeeRecord class.
ArrayList<String> eMp;
ArrayList<String> eMpL;
ArrayList<Double> eW;
// ArrayLists used to store values in setEmployeePayroll() method.
ArrayList<Integer> eId = new ArrayList<Integer>();
ArrayList<Double> hours = new ArrayList<Double>();
ArrayList<Double> tPay = new ArrayList<Double>();
// Instance Variables used in setEmployeePayroll() method.
private String employeeId = "%03d";
private int weekNumber = 0;
private double hoursWorked = 0.0;
private double hoursWorked2 = 0.0;
private int terminate = 1000;
private int i = 0;
Scanner in = new Scanner(System.in);
/**
* A method that implements the EmployeeRecord class to prompt the user to enter information for the payroll of each ACME employee. The payroll for each employee is then displayed.
*/
public void setEmployeePayroll()
{
// Constructs a new EmployeeRecord to implement classes from EmployeeRecord class.
EmployeeRecord e = new EmployeeRecord();
e.setEmployeeInfo();
eMp = e.getEmployeeArrayList();
eMpL = e.getEmployeeLastArrayList();
eW = e.getWageArrayList();
// Local variables used in this method.
double totalPay = 0.0;
double totalHours = 0.0;
double overTime = 0.0;
double overTime2 = 0.0;
System.out.println("Please enter ACME employee ID, the week they worked (1 or 2), and the number of hours worked. This information should be entered in the order the names were entered. Enter 0 when you are done inputing information.");
while(terminate != 0)
{
terminate = in.nextInt();
if(terminate == 0)
{
break;
}
weekNumber = in.nextInt();
if(weekNumber == 1)
{
hoursWorked = in.nextDouble();
}
else if(weekNumber == 2)
{
hoursWorked2 = in.nextDouble();
}
// Checks to see if an employee receives a 150% bonus on their payroll.
if(hoursWorked > 0 && hoursWorked <= 40 && hoursWorked2 > 0 && hoursWorked2 <= 40)
{
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (eW.get(i - 1));
tPay.add(totalPay);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked2 > 40 && hoursWorked > 0 && hoursWorked <= 40)
{
overTime2 = hoursWorked2 - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (eW.get(i - 1)) + (overTime2 * 1.5);
tPay.add(totalPay);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked > 40 && hoursWorked2 <= 40 && hoursWorked2 > 0)
{
overTime = hoursWorked - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (eW.get(i - 1)) + (overTime * 1.5);
tPay.add(totalPay);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
else if(hoursWorked > 40 && hoursWorked2 > 40)
{
overTime = hoursWorked - 40;
overTime2 = hoursWorked2 - 40;
totalHours = hoursWorked + hoursWorked2;
hours.add(totalHours);
totalPay = totalHours * (eW.get(i - 1)) + (1.5 * (overTime + overTime2));
tPay.add(totalPay);
hoursWorked = 0.0;
hoursWorked2 = 0.0;
}
i = terminate;
}
// Constructs a new date format for the date of the payroll.
DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date d = new Date();
// Gets current date and time for payroll.
System.out.println("ACME Payroll run on " + format.format(d));
System.out.println();
System.out.println("Employee Number | Employee Name | Hours Worked | Total Pay");
for(int i = 1; i <= e.getEmployeeArrayList().size(); i++)
{
System.out.println(String.format(employeeId, i) + " | " + eMp.get(i - 1) + " " + eMpL.get(i - 1) + " | " + hours.get(i - 1) + " | " + tPay.get(i - 1));
}
}
}
/**
* The main class that runs the contents of the EmployeeRecord and Employee classes.
*
*
*/
public class runPayroll
{
public static void main(String[] args)
{
Employee eE = new Employee();
eE.setEmployeePayroll();
}
}
/**
* Please enter the names of each ACME employee, each employee tax ID, and each employee wage rate. Press Q when you are done entering names.
Jane Smith 1010101 10
Employee ID | Employee Name | Tax ID | Wage
001 | Jane Smith | 1010101 | 10.00
John Smith 1111111 10
Employee ID | Employee Name | Tax ID | Wage
001 | Jane Smith | 1010101 | 10.00
002 | John Smith | 1111111 | 10.00
q
Please enter ACME employee ID, the week they worked (1 or 2), and the number of hours worked. This information should be entered in the order the names were entered. Enter 0 when you are done inputing information.
1 1 20
1 2 30
0
ACME Payroll run on 2015/09/15 22:25:19
Employee Number | Employee Name | Hours Worked | Total Pay
Exception in thread "main" 001 | Jane Smith | 50.0 | 500.0
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at Employee.setEmployeePayroll(Employee.java:128)
at runPayroll.main(runPayroll.java:11)
*/
答案 0 :(得分:0)
问题存在于Employee类的底部。
for(int i = 1; i <= e.getEmployeeArrayList().size(); i++)
{
System.out.println(String.format(employeeId, i) + " | " + eMp.get(i - 1) + " " + eMpL.get(i - 1) + " | " + hours.get(i - 1) + " | " + tPay.get(i - 1));
}
ArrayList
是零索引列表。
简而言之,想象一下,您要列出列表中的所有元素,因为大多数人会认为列表是:
1 2 3 4 5 6 7 8 9
这是一个单索引列表。零索引列表(计算机编程中使用的类型)的布局如下:
0 1 2 3 4 5 6 7 8
如果你想访问第一个元素,你必须得到位置0的元素。如果你想获得第5个元素,你必须得到位置4的元素。它有点倒退,但这就是方式它已经存在多年了,在处理列表和数组一段时间后它才有意义。出于使用零索引数组的原因,请参阅here。
至于代码,您尝试像单索引列表一样访问它:
for(int i = 1; i <= e.getEmployeeArrayList().size(); i++)
列表中只有一个条目,所以它看起来像这样:
0
for
循环开始迭代。它从int i = 1
开始。小问题:数组中没有1
索引。 Java不知道如何处理它,所以它会爆炸。要处理零索引列表,您应该使用
for(int i = 0; i < e.getEmployeeArrayList().size(); i++)