问题: 编写一个程序来计算公司中5个小时工薪酬员工的工资。您的计划将为5名员工中的每一位员工输入员工ID,他们工作的小时数和他/她的工资率。这些数据将存储在3个并行数组中:employee_ID,hours和payrate。然后,您的程序将调用一个方法来计算每个员工的工资,结果将存储在另一个并行数组中。并排输出4个并行阵列。然后,您的程序将调用3个方法:findAve确定5名员工的平均工资,findMax和findMin以确定并返回分别获得最高和最低工资的员工的ID。结果将以主要方法输出。
麻烦:如何实现方法调用以获得数组平均值?
代码:
public class Employees {
public static void main(String[] args) {
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
int[] id;
id = new int[5];
int[] payrate;
payrate = new int[5];
int[] wage;
wage = new int[5];
final int EMPLOYEES = 5; // Number of employees
int[] hours = new int[EMPLOYEES]; // Array of hours
System.out.println("Enter the hours worked by " +
EMPLOYEES + " employees.");
// Get the hours for each employee.
for (int index = 0; index < EMPLOYEES; index++)
{
System.out.print("Employee " + (index + 1) + ": ");
hours[index] = keyboard.nextInt();
}
System.out.println("The hours you entered are:");
// Display the values entered.
for (int index = 0; index < EMPLOYEES; index++)
System.out.println(hours[index]);
System.out.println("Enter the payrate worked by " +
EMPLOYEES + " employees.");
// Get the payrate for each employee.
for (int index1 = 0; index1 < EMPLOYEES; index1++)
{
System.out.print("Employee " + (index1 + 1) + ": ");
payrate[index1] = keyboard.nextInt();
}
System.out.println("The payrate you entered are:");
// Display the values entered.
for (int index1 = 0; index1 < EMPLOYEES; index1++)
System.out.println(hours[index1]);
System.out.println("Enter the wage worked by " +
EMPLOYEES + " employees.");
// Get the wage for each employee.
for (int index2 = 0; index2 < EMPLOYEES; index2++)
{
System.out.print("Employee " + (index2 + 1) + ": ");
wage[index2] = keyboard.nextInt();
}
System.out.println("The wage you entered are:");
// Display the values entered.
for (int index2 = 0; index2 < EMPLOYEES; index2++)
System.out.println(wage[index2]);
}
//A method that calculates and returns the average of a passed array.
public static void calculateAverage (int[] wage)
{
int average = 0;
int total = 0;
for (int i=0; i<=wage.length; i++)
{
total += wage[i];
}
average = (total / wage.length);
return;
}
System.out.println(average);
}
答案 0 :(得分:1)
你已经在Blocked loading mixed active content
完成了大部分工作。但是有几个问题。
您希望将for循环设置为https
而不是calculateAverage()
,因为数组从0开始,所以当实际数组的i<wage.length
时,您将得到一个超出范围的例外。即,如果有5名员工,<=
其中索引为i = length
。
你没有归还任何东西。你已计算出平均值,所以请将其归还。
您需要设置wage.length == 5
以外的返回类型。我还建议不要使用整数,以防你的平均值变成小数。您必须先计算0, 1, 2, 3, 4
值,然后再进行计算。
粗略修复:
void
之后您的int
也无法访问public static double calculateAverage (int[] wage) {
double average = 0;
int total = 0;
for (int i=0; i<wage.length; i++) {
total += wage[i];
}
average = (double) total / (double) wage.length;
return average;
}
,因为它超出了该方法的范围。你可以这样称呼它:
system.out.println
答案 1 :(得分:0)
您的calculateAverage方法返回void,这意味着它不会返回值。你应该让它返回平均值。
PS:对于货币价值,我建议你使用double而不是int。当int没有时,双倍接受十进制值。
public static int calculateAverage (int[] wage)
{
int average = 0;
int total = 0;
for (int i=0; i<wage.length; i++)
{
total += wage[i];
}
average = (total / wage.length);
return average;
}