Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何减少混乱?_Java_Arrays_Loops - Fatal编程技术网

Java 如何减少混乱?

Java 如何减少混乱?,java,arrays,loops,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.pri

我正在写一个程序,用户在数组中输入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.");
}

}
import java.util.Scanner;
公营退休{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int[][]年龄=新int[8][2];
System.out.println(“输入员工当前年龄(19到70岁之间),并输入他们期望的退休年龄(62、66或70岁)。”;
for(int i=0;i

现在,我想弄清楚的是,它们是否可能是减少一些混乱的一种方法,但我不确定我是否可以,或者它是否必须占用空间。这两种方式都无所谓,但我希望它看起来更流线型,而不是那么凌乱。谢谢你的帮助

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

您似乎已经能够使用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.");
}
您是否意识到,附加了
*
的部件似乎每次增加一个,而所有其他部件保持不变?这意味着我们可以将其转换为for循环:

for (int i = 0 ; i < employees.length ; i++) {
    employees[i] = (ages[i][1] - ages[i][0]);
}
同样,你会看到一些数字在一个接一个地增加,而其他的一切都保持不变。我们也可以将其更改为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.");
}
for(int i=0;i

但是,员工人数并没有从0增加到7。相反,它是从1到8。这就是为什么它是
“Employee#”+(i+1)
而不是
“Employee#”+i

它看起来像java,而不是javascript。老实说,我不知道它们之间有什么区别。我使用的是eclipse,很可能是Java,而不是javascript,抱歉。这有点奇怪:你可以使用twodim数组。。。但是你也不会想到为员工使用数组,相反,你写代码8次也没有问题?!意思只有你有var1 var2。。。多达8个阵列的尖叫声…嗯。。。我在工作休息,下班后我会试试这个,然后发布我的代码,我会看看我是否能用它来解决这个问题,我只是对如何让每次通过的次数递增有点困惑。也许将它们分配给一个变量,并在每次传递时递增它?比如年龄[t][0],然后将t声明为int=0并递增。行吗?好吧,我试过了。我收到一个错误,说employee不能解析为变量。我所做的一切似乎都无法解决这个问题。我已经尝试过预先声明变量,但似乎没有任何效果。有什么提示吗?你使用了正确的代码吗?第一个代码片段就是您所需要的@senpaimaster@senpaimaster我想你错过了这一行:
int[]employees=newint[8]
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 (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.");
}