2016-04-22 11 views
1

与えられたID番号を持つ従業員の賃金率と時間を取得し、配列に格納し、メソッドを使用して、従業員の総賃金も配列に格納されます。私のプログラムは従業員の給与と時間を受け入れることができますが、従業員IDとそれに対応する総給与を出力すると、[D @ 1909752]のような奇妙な文字群を返します。特定の従業員の総賃金を正しく出力するには、どうすればよいのですか?私はこの問題が、メソッドと最終的なforループにメソッドを渡すことと、私のメソッドがintにdoubleを掛けるという事実との間にどこかで発生すると思いますが、ここから何をするのか分かりません。ご協力ありがとうございました。私のコードは以下の通りです。forループ出力をメソッドから配列の適切な値にする方法

import java.util.Scanner; 

class Payroll 
{ 
    public static void main(String[] args) 
    { 
     int[] employeeId = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489}; 

     int[] hours = new int[7]; 

     double[] payRate = new double[7]; 

     for(int counter = 0; counter< 7; counter++) 
     { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Enter the hours worked by employee #" + employeeId[counter]); 
     hours[counter] = keyboard.nextInt(); 

      if (hours[counter] < 0) 
      { 
       System.out.println("Error: hours cannot be negative. The program will now close."); 
       System.exit(0); 
      } 
      else 
      { 
       continue; 
      } 
     } 

     for(int counter1 = 0; counter1< 7; counter1++) 
     { 
     Scanner keyboard = new Scanner(System.in); 
     System.out.println("Enter the payrate of employee #" + employeeId[counter1]); 
     payRate[counter1] = keyboard.nextDouble(); 

      if (payRate[counter1] < 6.0) 
      { 
       System.out.println("Error: payrate must be above 6.0. The program will now close."); 
       System.exit(0); 
      } 
      else 
      { 
       continue; 
      } 
     } 

     for (int counter2 = 0; counter2 < 7; counter2++) 
     { 
     System.out.println("The gross pay for employee #" + employeeId[counter2] + " is " + wages(employeeId, hours, payRate)); 
     } 
    } 

    public static double[ ] wages(int[] employeeId, int[] hours, double[] payRate) 
    { 
     double[] wages = new double[7]; 

     for(int counter2 = 0; counter2< 7; counter2++) 
     { 
      wages[counter2] = hours[counter2] * payRate[counter2]; 
     } 

     return wages; 
    } 

} 
+0

'wages(employeeId、hours、payRate)[counter2]' – jgitter

+0

いいえ、 'counter2'はその時点では表示されません。 「賃金」にはローカルです。 –

+0

私はメインメソッドで 'counter2'を使って私にはあまり混乱させないようにしたので働いた –

答えて

1

ストリング[[email protected]doubleのアレイに適用toString()のデフォルトの出力です。声明

System.out.println("The gross pay for employee #" + employeeId[counter2] + " is " + wages(employeeId, hours, payRate)); 

doubleの配列を返しますwages方法とでは、配列全体ではなく、ある特定の要素を印刷するプログラムを語りました。 7つの要素すべてを印刷する場合は、ループまたはラムダを使用して各要素を個別に印刷する必要があります。

+0

' wages(employeeId、hours、payRate)[counter2] ' – jgitter

+0

ありがとう、そして、完全に働いたジッター。皆さん、ありがとうございました! –

関連する問題