传递给重载函数 - java

时间:2016-04-01 23:55:10

标签: java methods overloading

我正在编写一个小程序,根据员工类型和工作小时数(加班与非加班)计算税额。我将包含我的代码的完整副本。

import java.util.Scanner;

public class Payroll
{   
    Scanner input = new Scanner(System.in);

        private int empType;
        private double contractorId;
        private double empPayRate;
        private double empHours;
        private double empOtHours;
        private double empOtPay;
        private double fedTax;
        private double stateTax;
        private double empGrossPay;
        private double empNetPay;
        private double empFedTax;
        private double empStateTax;
        private double empTotalTax;
        private double empRegPay;

    public int empTypeCalc()
    //calculate employee type

    {
        System.out.printf("What type of employee is this?%nEnter 1 for regular employee and 2 for contractors, or 3 to quit: ");
        int empType = input.nextInt();

            if (empType == 1)
            {
            System.out.printf("Input employee's pay rate:%n");
            empPayRate = input.nextDouble();
            System.out.printf("Input employee's hours worked:%n");
            empHours = input.nextDouble();

            grossPayCalc(empPayRate, empHours);
            }

            else if (empType == 2)
            {
            System.out.printf("Input contractor's pay rate:%n");
            empPayRate = input.nextDouble();
            System.out.printf("Input contractors's hours worked:%n");
            empHours = input.nextDouble();
            System.out.printf("Input contractors's ID number:%n");
            contractorId = input.nextInt();

            grossPayCalc(empPayRate, empHours, contractorId);
            }

            else if (empType == 3)
            {
            System.out.printf("Ending program. Have a nice day.%n");
            }

            else
            {
            System.out.printf("Invalid employee type entered please retry.%n");
            empTypeCalc();
            }
            return empType;
    }

    public double grossPayCalc(double empPayRate, double empHours)
    //calculate regular employee pay

    {
        if (empHours <= 40)
        {
            empGrossPay = empPayRate * empHours;

            taxCalc(empHours, empPayRate, empGrossPay);
        }
        else
        {
            otPayCalc(empHours, empPayRate, empGrossPay);
        }
        return empPayRate;
    }


    public double otPayCalc(double empHours, double empPayRate, double empGrossPay)
    //calculate Overtime pay for regular employees

    {   
        if (empHours <= 65)
        {
            empOtHours = empHours - 40;
            empRegPay = 40 * empPayRate;
            empOtPay = empOtHours * empPayRate * 1.5;
            empGrossPay = empOtPay + empRegPay;

            otTaxCalc(empOtPay, empOtHours, empRegPay, empGrossPay, empHours, empPayRate);
        }
        else if (empHours > 65)
        {
            System.out.println("Employee May not work more than 65 Hours.%n");
            empTypeCalc();
        }
        return empOtHours;
    }

    public double grossPayCalc(double empPayRate, double empHours, double contractorId)
    //calculate contractor pay

    {
        if (empHours <= 50)
        {
            empGrossPay = empPayRate * empHours;
            System.out.printf("Contractor's gross pay is $%.2f.%n%n", empGrossPay);
            empTypeCalc();
        }
        else
        {
            System.out.printf("Contractor may not work more than 50 hours per week. Please try again.%n%n");
            empTypeCalc();
        }
        return empPayRate;
    }

    public double otTaxCalc(double empOtPay, double mpOtHours, double empRegPay, double empGrossPay, double empHours, double empPayRate)
    //calc tax for regular employees

    {
            System.out.printf("Please enter Federal Tax Rate. (e.g. 7.5 would be 7.5%)%n");
            fedTax = input.nextDouble();
            System.out.printf("Please enter State Tax Rate. (e.g. 7.5 would be 7.5%)%n");
            stateTax = input.nextDouble();

            empFedTax = (empGrossPay / 100) * fedTax;
            empStateTax = (empGrossPay / 100) * stateTax;
            empTotalTax = empStateTax + empFedTax;
            empNetPay = empGrossPay - empTotalTax;

            System.out.printf("Employee's Federal tax ammount is $%.2f.%n", empFedTax);
            System.out.printf("Employee's State tax ammount is $%.2f.%n", empStateTax);
            System.out.printf("Employee's total tax ammount is $%.2f.%n", empTotalTax);
            System.out.printf("Employee's Federal Tax ammount is $%.2f.%n", empNetPay);
            empTypeCalc();

            return empFedTax;
    }

    public double taxCalc(double empHours, double empPayRate, double empGrossPay)
    {
            System.out.printf("Please enter Federal Tax Rate. (e.g. 7.5 would be 7.5%)%n");
            fedTax = input.nextDouble();
            System.out.printf("Please enter State Tax Rate. (e.g. 7.5 would be 7.5%)%n");
            stateTax = input.nextDouble();

            empFedTax = (empGrossPay / 100) * fedTax;
            empStateTax = (empGrossPay / 100) * stateTax;
            empTotalTax = empStateTax + empFedTax;
            empNetPay = empGrossPay - empTotalTax;

            System.out.printf("Employee's Federal tax ammount is $%.2f.%n", empFedTax);
            System.out.printf("Employee's State tax ammount is $%.2f.%n", empStateTax);
            System.out.printf("Employee's total tax ammount is $%.2f.%n", empTotalTax);
            System.out.printf("Employee's Federal Tax ammount is $%.2f.%n", empNetPay);
            empTypeCalc();

            return empFedTax;
    }
}

我的第一个功能是你输入了什么类型的员工(承包商或常规)然后询问一些基本信息(empPayRate和empHours)传递给我的grossPayCalc的部分,正常员工似乎有错误。这是传递给grossPayCalc的代码。

    if (empType == 1)
    {
    System.out.printf("Input employee's pay rate:%n");
    empPayRate = input.nextDouble();
    System.out.printf("Input employee's hours worked:%n");
    empHours = input.nextDouble();

    grossPayCalc(empPayRate, empHours);
    }

这应该传递给

public double grossPayCalc(double empPayRate, double empHours)
//calculate regular employee pay

{
    if (empHours <= 40)
    {
        empGrossPay = empPayRate * empHours;

        taxCalc(empHours, empPayRate, empGrossPay);
    }
    else
    {
        otPayCalc(empHours, empPayRate, empGrossPay);
    }
    return empPayRate;
}

之后我收到错误。

What type of employee is this?
Enter 1 for regular employee and 2 for contractors, or 3 to quit: 1
Input employee's pay rate:
50
Input employee's hours worked:
40
Exception in thread "main" java.util.UnknownFormatConversionException: Conversio
n = ')'
        at java.util.Formatter.checkText(Unknown Source)
        at java.util.Formatter.parse(Unknown Source)
        at java.util.Formatter.format(Unknown Source)
        at java.io.PrintStream.format(Unknown Source)
        at java.io.PrintStream.printf(Unknown Source)
        at Payroll.taxCalc(Payroll.java:146)
        at Payroll.grossPayCalc(Payroll.java:72)
        at Payroll.empTypeCalc(Payroll.java:36)
        at PayrollTest.main(PayrollTest.java:6)

我不确定导致此错误的原因。从方法重载传递方法/这是一个错误吗?我很想知道导致这个错误的原因,以及如何防止它在将来发生。

3 个答案:

答案 0 :(得分:0)

问题似乎是由方法System.out.printf("Please enter Federal Tax Rate. (e.g. 7.5 would be 7.5%)%n");中的taxCalc引起的。
您可能希望打印7.5%,但百分号将被解释为格式说明符。
试试...would be 7.5%%)...

答案 1 :(得分:0)

问题在于此方法taxCalc

尝试添加%符号以逃避%符号。

所以,只需替换这一行:

System.out.printf("Please enter Federal Tax Rate. (e.g. 7.5 would be 7.5%)%n");

System.out.printf("Please enter Federal Tax Rate. (e.g. 7.5 would be 7.5%%)%n");

然后为其他类似的行做同样的事情。

答案 2 :(得分:0)

在taxCalc方法中,您有一个百分号和一个括号。您应该使用双百分号使其工作,如下所示:

System.out.printf("Please enter Federal Tax Rate. (e.g. 7.5 would be 7.5%%)%n");