如何减少混乱?

时间:2016-12-04 20:33:30

标签: java arrays loops

我正在编写一个程序,用户将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.");
}

}

现在,我想弄清楚的是,如果它们可能是一种减少这种混乱的方法,但我不确定我是否可以或者它是否只需要占用空间。这两种方式无关紧要,但我希望它看起来更精简,而不是那么混乱。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

我首先将employee作为一个数组,然后从那里创建一个for循环。

答案 1 :(得分:0)

您似乎已经可以使用for循环,因此重构此代码应该不是问题:

Scanner input = new Scanner(System.in);

int[][] ages = new int[8][2];
int[] employees = new int[8];
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();
}
for (int i = 0 ; i < employees.length ; i++) {
    employees[i] = (ages[i][1] - ages[i][0]);
}

for (int i = 0 ; i < employees.length ; i++) {
    System.out.println("The current age and desired retirement age for Employee #" + (i + 1) + " is: " + (ages[i][0]) + " and " + (ages[i][1]) + ".");
    System.out.println("Employee #" + (i + 1) + " has to work " + (employees[i]) + " years before they can retire.");
}

为了更容易访问员工变量,我们将它们放在一个名为employees的数组中。

以下是重构代码的方法:

寻找模式!在这些方面:

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]);

你是否意识到附加*的部分每次增加1,而所有其他部分保持不变?这意味着我们可以将其转换为for循环:

for (int i = 0 ; i < employees.length ; i++) {
    employees[i] = (ages[i][1] - ages[i][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.");

同样,你会看到一些数字一个接一个地增加而其他一切都保持不变。我们也可以将其更改为for循环:

for (int i = 0 ; i < employees.length ; i++) {
    System.out.println("The current age and desired retirement age for Employee #" + (i + 1) + " is: " + (ages[i][0]) + " and " + (ages[i][1]) + ".");
    System.out.println("Employee #" + (i + 1) + " has to work " + (employees[i]) + " years before they can retire.");
}

但是,员工编号不会从0增加到7.而是从1到8.这就是原因"Employee #" + (i + 1)而不是"Employee #" + i