2

我的程序的目的是显示员工(经理编码器)的工资,具体取决于该员工在一个月内工作了多少小时。包含 3 名员工的数组作为参数转到会计。会计返回每个员工的工资。编码员和经理的薪水以不同的方式计算。

这是我的Java代码:

    public class OOP
    {  
       public static void main(String[] args)
       {  
    // fill the staff array with three Employee objects.


    Employee[] staff = new Employee[3];

    staff[0] = new Manager("Carl Cracker",75000.00,(short)160);
    staff[1] = new Coder("Harry Hacker", 50000.00,(short)165);
    staff[2] = new Coder("Tony Tester", 40000.00,(short)200);

    Accountant Counter = new Accountant();
    Counter.printSalary(staff, (byte)2);

       }
    }

    abstract class Employee
    {  
       public Employee(String n, double w, short h)
    {  
      name = n;

      wageRate = w;

      workedHours = h;

    }

     public String getName()
     {
         return name;
     }

     public double getSalary(short workingHours)
     {

        this.salary = this.wageRate * (this.workedHours/workingHours);
        return salary;
     }

     private String name;
     protected double wageRate;
     protected short workedHours;
     protected double salary;
    }
    class Coder extends Employee
    {
        public Coder (String n, double w, short h)
        {
            super( n,  w,  h);
        }

        public double getSalary(short workingHours)
        {
            return super.getSalary(workingHours);
        }
    }
    class  Manager extends Employee
    {
        public Manager (String n, double w, short h)
        {
            super( n,  w,  h);
        }
        public double getSalary(short workingHours)
        {
            if (workingHours > workedHours)
            return  super.getSalary(workingHours)*0.7;
            else
                {
                   salary = wageRate;
                   return salary;
                }


         }
     }
    class Accountant
    {
        public Accountant ()
        {
        }
        public void printSalary(Employee[] emp, byte monthInd)
        {
            Employee[] workers = emp;
            byte MID = monthInd;
            MID = (byte) (MID - 1);
            for (Employee worker : workers)
                {
                    System.out.printf("%s's salary this month is %8.2f",worker.getName(), worker.getSalary(workingHours[MID]));
                    System.out.println();
                }
        }

        private short[] workingHours = new short[]{168,160,160,172,162,154,184,168,168,184,168,172};
     }

预期结果:

Carl Cracker 这个月的工资是 75000

Harry Hacker 这个月的工资是 51562.5

Tony Tester 这个月的工资是 50000

实际结果:

Carl Cracker 这个月的薪水是 75000,00

Harry Hacker 这个月的薪水是 50000,00

Tony Tester 这个月的薪水是 40000,00

有人可以解释一下为什么吗?

4

1 回答 1

4

你是 Java 中整数除法的受害者。

即使有short值,也适用整数除法(JLS 15.17.2),其中非整数结果会被截断。第二个月,索引 1,是 160 个工作小时。Tony Tester 有 200 小时。在普通数学中,200 / 160 = 1.25,但在 Java 整数除法中,200 / 160 = 1。这就是为什么你会看到 40000 而不是 50000。

在 Employee 的getSalary方法中,将其中一个值转换为 adouble以获得浮点除法,这样结果就是你想要的。

this.salary = this.wageRate * ((double) this.workedHours/workingHours);

您的代码输出:

$ java OOP
Carl Cracker's salary this month is 75000.00
Harry Hacker's salary this month is 50000.00
Tony Tester's salary this month is 40000.00

更改代码的输出:

$ java OOP
Carl Cracker's salary this month is 75000.00
Harry Hacker's salary this month is 51562.50
Tony Tester's salary this month is 50000.00
于 2013-04-24T22:06:05.957 回答