1

this is my first time on stackoverflow and I had a question.

I am to design a program that prints out payroll for 5 employees.

We are required to have one array of type int for the Employee ID...

int[ ] {100,200,300,400,500};

And a two dimensional array for the payroll... int[5][5]

We are given the Employee ID's, hrs worked and pay rate for each employee, which are to be hardcoded within the corresponding array elements.

For example

payroll[0][0] is Employee 1's hrs worked, which in this case is 50

payroll[1][0] is Employee 1's pay rate, which in this case is 25

payroll[2][0] should be Employee 1's gross pay

My question is, I need to calc gross pay for each employee and store those values into the 3rd column in payroll[ ][ ].

Below is the code I have so far, any help would be amazing.

  public class CIS131_HW5
     {
     public static void main(String[] args)
     {
      // Employee ID array
      int[] ID = new int[] {100,200,300,400,500};

      for (int i=0; i<ID.length; i++)
      {
       System.out.println(ID[i]);
      }
      System.out.println( "" );

      // Payroll Array
     int[][] payroll = new int [5][5];
     int rows = 5;
     int columns = 5;

    // values given for hrs worked
    payroll[0][0] = 50;
    payroll[0][1] = 15;
    payroll[0][2] = 48;
    payroll[0][3] = 40;
    payroll[0][4] = 40;

    // values given for pay rate
    payroll[1][0] = 25;
    payroll[1][1] = 15;
    payroll[1][2] = 27;
    payroll[1][3] = 25;
    payroll[1][4] = 23;

    for (int i=0; i<rows; i++)
   {
    for(int j=0; j<columns; j++)
   {
    System.out.println(payroll[i][j] + " ");
   }
    System.out.println( "" );
    }
   }
   }
4

1 回答 1

0

您需要使用 for 循环中的变量。下面是工作代码。

    public static void main (String[] args){

    // Employee ID array
    int[] ID = new int[] {100,200,300,400,500};
    // Payroll Array
    int[][] payroll = new int [5][5];

    int rows = 5;
    int cols = 1;
      // values given for hrs worked
    payroll[0][0] = 50;
    payroll[0][1] = 15;
    payroll[0][2] = 48;
    payroll[0][3] = 40;
    payroll[0][4] = 40;

    // values given for pay rate
    payroll[1][0] = 25;
    payroll[1][1] = 15;
    payroll[1][2] = 27;
    payroll[1][3] = 25;
    payroll[1][4] = 23;


    for (int i=0; i<cols; i++){
        for(int j=0; j<rows; j++){
            System.out.println("user id:" + ID[j] + " | " + "  hours: " + payroll[i][j] + "  rate: " + payroll[i + 1][j]);
        }
    }
}
于 2016-03-05T21:22:29.040 回答