【问题标题】:How to reduce clutter?如何减少杂乱?
【发布时间】: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 为数组尖叫...

标签: java arrays loops


【解决方案1】:

我会先将 employee 设为一个数组,然后可以从那里创建一个 for 循环。

【讨论】:

  • 嗯...我在工作休息,下班后我会尝试这个并发布我的代码,我会看看我是否能用它来解决这个问题,我只是有点困惑我如何获得数字以增加每次传递。也许将它们分配给一个变量并在每次传递时递增它?像 age[t][0] 然后将 t 声明为 int = 0 并递增它。这行得通吗?
【解决方案2】:

你似乎已经可以使用 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]);

您是否意识到,附有* 的部分似乎每次都增加一个,而所有其他内容都保持不变?这意味着我们可以把它变成一个 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

【讨论】:

  • 好的,我试过了。我收到一条错误消息,说员工无法解析为变量。我所做的一切似乎都无法解决这个问题。我已经尝试过事先声明变量,但似乎没有任何效果。有什么建议吗?
  • 您是否使用了正确的代码?第一个代码 sn-p 就是您所需要的。 @senpaimaster
  • @senpaimaster 我想你错过了这一行:int[] employees = new int[8];
猜你喜欢
  • 2011-08-11
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多