2016-12-04 6 views
-4

私は、ユーザーが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."); 
} 

} 

は今、私が把握しようとしていることですが、それだけに持っている場合、私は私ができるのかはわかりませんかスペースを取る。いずれにしても問題はありませんが、もう少し合理化されて見えるようにしたいと思います。助けてくれてありがとう!

+0

javascriptではなく、javaのように見えます。 –

+0

正直言って、私は彼らの違いを知らなかった。私は日食を使用しています、それはおそらくJavaとjavascript、申し訳ありません。 – senpaimaster

+2

これは奇妙なことです:あなたは年齢のためにtwodim配列を使っても問題ありません...しかし、従業員のために配列を使用することはありません。代わりにコードを8回書くのに問題はありませんか? – GhostCat

答えて

0

まず、employeeを配列にしてから、そこからforループを作成します。

+0

ええと...私は仕事中で休憩中です。仕事をした後でこれを試してコードを投稿します私はそれを使用して理解することができるかどうかを見てみましょう。私は、各パスをインクリメントするための数字をどうやって得るのか少し混乱させます。多分それらを変数に代入し、それぞれのパスをインクリメントしますか? age [t] [0]のようにして、tをint = 0に宣言し、それをインクリメントします。それは働くだろうか? – senpaimaster

0

あなたは既にループのために使用することができるように見えるので、このコードをリファクタリングすることは問題になることはありません。

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

今、あなたは数字の一部が1しばらくずつ増加していることがわかり、このコード再び

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."); 
} 

しかし、従業員番号が代わりに0から7まで増加しない、それは1から、それは"Employee #" + (i + 1)代わりの"Employee #" + iである理由だ8にあります。

+0

さて、私はこれを試しました。従業員を変数に解決できないというエラーが表示されます。私はこれを修正するように見えることはありません。私は手で変数を宣言しようとしましたが、何も動かないようです。任意のヒント? – senpaimaster

+0

正しいコードを使用しましたか?最初のコードスニペットが必要なものです。 @senpaimaster – Sweeper

+0

@senpaimaster私はあなたがこの行を逃したと思う: 'int [] employees = new int [8];' – Sweeper

関連する問題