【问题标题】:Method Call on an Array to get an Average对数组进行方法调用以获取平均值
【发布时间】:2016-01-29 01:08:35
【问题描述】:

问题: 编写一个程序来计算公司 5 名按小时计酬的员工的工资。您的程序将为 5 名员工中的每一个输入员工 ID、他们工作的小时数和他/她的工资率。这些数据将存储到 3 个并行数组中:employee_ID、hours 和 payrate。然后您的程序将调用一个方法来计算每个员工的工资,结果将存储在另一个并行数组中。并排输出 4 个并行阵列。然后,您的程序将调用 3 个方法:findAve 确定 5 名员工的平均工资,findMax 和 findMin 确定并返回分别获得最高和最低工资的员工的 ID。结果会在main方法中输出。

问题:如何实现方法调用以获取数组平均值?

代码:

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

【问题讨论】:

  • 为什么有这么多信息?你能只留下必要的部分,而不把整个背景带入问题吗? (您的实际问题可以放在 4-5 行中)
  • 如果您的 calculateAverage 方法旨在返回平均值,则它可能不应该是 void。同样i&lt;=wage.length 将抛出IndexOutOfBoundsException。应该是i&lt;wage.length

标签: java arrays


【解决方案1】:

您已经在calculateAverage() 中完成了大部分工作。但是有几个问题。

  1. 您希望将 for 循环设置为 i&lt;wage.length 而不是 &lt;=,因为数组从 0 开始,所以当实际数组的 i = length 时,您会得到一个越界异常。即,如果有 5 名员工,则为 wage.length == 5,其中索引为 0, 1, 2, 3, 4

  2. 你没有返回任何东西。你已经计算了平均值,所以返回它。

  3. 您需要设置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 之后也无法访问average,因为它超出了该方法的范围。你可以这样称呼它:

System.out.println(calculateAverage(wage));

【讨论】:

  • 这会将平均值转换为 double 进行 int 计算之后。要正确计算,您需要average = (double)total/(double)wage.length;
【解决方案2】:

您的 calculateAverage 方法返回 void,这意味着它没有返回值。你应该让它返回平均值。

PS:对于货币值,我建议你使用 double 而不是 int。当 int 不接受时,double 接受十进制值。

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;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-03
    • 2017-03-01
    • 1970-01-01
    • 2021-06-23
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多