public static void main(String[] args) {
final int EMPLOYEES = 3;
int[] hours = new int[EMPLOYEES];
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the hours worked by"+ " " + EMPLOYEES + " " + "employees.");
System.out.print("Employee 1:");
hours[0] = keyboard.nextInt();
System.out.print("Employee 2:");
hours[1] = keyboard.nextInt();
System.out.print("Employee 3:");
hours[2] = keyboard.nextInt();
System.out.println("The hours you enter are:");
System.out.println(hours[0]);
System.out.println(hours[1]);
System.out.println(hours[2]);
}
我们也可以使用循环来实现这个程序的运行:
public static void main(String[] args) {
final int EMPLOYEES = 6;
int [] hours = new int[EMPLOYEES];
Scanner keyboard = new Scanner(System.in);
System.out.println("Country name and population");
for (int index = 0; index<EMPLOYEES; index++)
{
System.out.print("Employee" + (index + 1) + " : ");
hours[index] = keyboard.nextInt();
}
System.out.print("The hours you entered are:");
for(int index = 0; index<EMPLOYEES; index++)
System.out.println(hours[index]);
}