我正在开发一个程序,该程序使用给定ID号的员工的工资率和小时数,将它们存储在数组中,并使用一种方法来确定员工的总工资,这也将存储在一个数组中。我的程序可以接受员工的工资率和工时,但是当它输出员工ID和相应的总工资时,它会返回一组奇数字符,如[D @ 1909752。如何让程序正确输出给定员工的总薪酬?我认为问题出现在方法和将方法传递给final for循环之间,以及我的方法将int乘以double的事实,但我不知道该怎么做。谢谢您的帮助。我的代码如下。
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 :(得分:1)
字符串[D@1909752
是应用于toString()
数组的double
的默认输出。在声明中
System.out.println("The gross pay for employee #" + employeeId[counter2] + " is " + wages(employeeId, hours, payRate));
方法wages
返回一个double
数组,并告诉程序打印整个数组,而不是一个特定元素。如果要打印所有七个元素,则需要使用循环或lambda单独打印每个元素。