工资核算数组Java计算

时间:2018-05-06 03:33:53

标签: java

大家好,这是我第一次在这里发帖,我刚开始学习java。这是我的任务,我需要用数组编写工资单代码。但是,我不明白为什么我不能让它工作。不知何故,它只计算最后一个员工,第一个和第二个不包括在内。如果你们能帮助我,我会很感激。谢谢!

public class ArrayIG 
{

    public static void main(String[] args) 
    {
        final int NUM_EMPLOYEES = 3;

        //creating array

        int[]hours = new int[NUM_EMPLOYEES];
        int[] employeeID = {5678459, 4520125, 7895122};
        double payRate;
        double wages = 0;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter your " + NUM_EMPLOYEES + " employees work hours and pay rate:");


        //get the hours
        for (int i = 0; i < NUM_EMPLOYEES; i++)
        {
            System.out.print("Employee #" + employeeID[i] + ": ");
            hours[i] = keyboard.nextInt();

            //get the hourly pay rate
            System.out.print("Enter the pay rate: ");
            payRate = keyboard.nextDouble();

            wages = hours[i] * payRate;
        }



        //display wages
        System.out.println("The hours and pay rates you entered are:");

        for(int i = 0; i < NUM_EMPLOYEES; i++)
        {

            System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeID[i], wages);
        }

    }

}

我的输出:

Enter your 3 employees work hours and pay rate:
Employee #5678459: 35
Enter the pay rate: 21
Employee #4520125: 37
Enter the pay rate: 18.5
Employee #7895122: 39
Enter the pay rate: 37.25
The hours and pay rates you entered are:
The total wages for Employee #5678459 is $1452.75
The total wages for Employee #4520125 is $1452.75
The total wages for Employee #7895122 is $1452.75

4 个答案:

答案 0 :(得分:0)

要么创建一系列工资,要么在工资正在印刷的循环中计算工资。你应该自己做作业

答案 1 :(得分:0)

您有3名员工 - &gt; 3工资。

但目前你只使用一个变量来保持工资:double wages = 0;

因此,每个循环都会替换它的值。

你应该创建一个长度为3的数组来存储工资:

并在你的循环中,替换

wages = hours[i] * payRate;

使用

wages[i] = hours[i] * payRate;

并打印:

System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeID[i], wages[i]);

答案 2 :(得分:0)

您正在设置每次迭代的工资率。即你只记录一个单一的工资状态。然后你迭代并显示一个工资变量,这将始终是最后一次计算。

存储每个&#34;工资&#34;数组中的值就像你已经完成了几个小时,你应该解决你的问题。

答案 3 :(得分:0)

您正在收集3个不同的小时,但只将它们存储在一个值中。工资也一样。将它们存储为数组时会发生什么?

import java.util.Scanner;

public class ArrayIG 
{
    public static void main(String[] args) 
    {
        final int NUM_EMPLOYEES = 3;

        //creating array
        int[] hours = new int[NUM_EMPLOYEES];
        int[] employeeID = {5678459, 4520125, 7895122};
        double[] payRate = new double[NUM_EMPLOYEES];
        double[] wages = new double[NUM_EMPLOYEES];

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter your " + NUM_EMPLOYEES + " employees work hours and pay rate:");

        //get the hours
        for (int i = 0; i < NUM_EMPLOYEES; i++)
        {
            System.out.print("Employee #" + employeeID[i] + ": ");
            hours[i] = keyboard.nextInt();

            //get the hourly pay rate
            System.out.print("Enter the pay rate: ");
            payRate[i] = keyboard.nextDouble();

            wages[i] = hours[i] * payRate[i];
        }



        //display wages
        System.out.println("The hours and pay rates you entered are:");

        for(int i = 0; i < NUM_EMPLOYEES; i++)
        {
            System.out.printf("The total wages for Employee #%d is $%.2f\n", employeeID[i], wages[i]);
        }

    }

}