与えられた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;
}
}
'wages(employeeId、hours、payRate)[counter2]' – jgitter
いいえ、 'counter2'はその時点では表示されません。 「賃金」にはローカルです。 –
私はメインメソッドで 'counter2'を使って私にはあまり混乱させないようにしたので働いた –