【发布时间】:2016-12-04 20:33:29
【问题描述】:
我正在编写一个程序,其中用户将 8 个不同的员工年龄和期望的退休年龄输入到一个数组中,然后我会显示该信息以及他们退休的年数。这是我的代码:
import java.util.Scanner;
public class Retirement {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[][] ages = new int[8][2];
System.out.println("Enter the employees current ages (between 19 and 70), and enter their desired retirement ages (62, 66, or 70).");
for (int i = 0; i < ages.length; i++) {
ages[i][0] = input.nextInt();
ages[i][1] = input.nextInt();
}
int employee1 = (ages[0][1] - ages[0][0]);
int employee2 = (ages[1][1] - ages[1][0]);
int employee3 = (ages[2][1] - ages[2][0]);
int employee4 = (ages[3][1] - ages[3][0]);
int employee5 = (ages[4][1] - ages[4][0]);
int employee6 = (ages[5][1] - ages[5][0]);
int employee7 = (ages[6][1] - ages[6][0]);
int employee8 = (ages[7][1] - ages[7][0]);
System.out.println("The current age and desired retirement age for Employee #1 is: " + (ages[0][0]) + " and " + (ages[0][1]) + ".");
System.out.println("Employee #1 has to work " + (employee1) + " years before they can retire.");
System.out.println("\nThe current age and desired retirement age for Employee #2 is: " + (ages[1][0]) + " and " + (ages[1][1]) + ".");
System.out.println("Employee #2 has to work " + (employee2) + " years before they can retire.");
System.out.println("\nThe current age and desired retirement age for Employee #3 is: " + (ages[2][0]) + " and " + (ages[2][1]) + ".");
System.out.println("Employee #3 has to work " + (employee3) + " years before they can retire.");
System.out.println("\nThe current age and desired retirement age for Employee #4 is: " + (ages[3][0]) + " and " + (ages[3][1]) + ".");
System.out.println("Employee #4 has to work " + (employee4) + " years before they can retire.");
System.out.println("\nThe current age and desired retirement age for Employee #5 is: " + (ages[4][0]) + " and " + (ages[4][1]) + ".");
System.out.println("Employee #5 has to work " + (employee5) + " years before they can retire.");
System.out.println("\nThe current age and desired retirement age for Employee #6 is: " + (ages[5][0]) + " and " + (ages[5][1]) + ".");
System.out.println("Employee #6 has to work " + (employee6) + " years before they can retire.");
System.out.println("\nThe current age and desired retirement age for Employee #7 is: " + (ages[6][0]) + " and " + (ages[6][1]) + ".");
System.out.println("Employee #7 has to work " + (employee7) + " years before they can retire.");
System.out.println("\nThe current age and desired retirement age for Employee #8 is: " + (ages[7][0]) + " and " + (ages[7][1]) + ".");
System.out.println("Employee #8 has to work " + (employee8) + " years before they can retire.");
}
}
现在,我想弄清楚它们是否可以减少一些混乱,但我不确定我是否可以,或者它是否只需要占用空间。无论哪种方式都没有关系,但我希望它看起来更流线型,而不是那么混乱。感谢您的帮助!
【问题讨论】:
-
它看起来像 java,而不是 javascript。
-
说实话,我不知道他们有什么不同。我正在使用 eclipse,它可能是 Java 而不是 javascript,对不起。
-
这有点奇怪:您可以使用 twodim 数组来表示年龄...但是您也不会想到将数组用于员工,而是写下来没有问题代码8次?!
-
意思...只有你有 var1 var2...最多 var8 为数组尖叫...