找出处理用户多少次?

时间:2018-11-26 02:01:11

标签: java loops methods

我在部分作业上遇到了麻烦。在循环运行一次后,它询问用户是否要处理另一个雇员时,我必须查看我的程序中雇员被处理了多少次。如果他们输入y表示是,然后在第二次计算雇员之后输入n表示结束。我想说“已处理的雇员数:2”。我该怎么办?

package paytime;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scn = new Scanner(System.in);

        String firstName, lastName, choice;
        double hoursWorked, hourlyWage;
        boolean processAnotherEmployee = true;
        Employee one = new Employee();


        while(true)
        {
            if (processAnotherEmployee) 
            {
        System.out.print("Enter Y to process employee or any other key to end: ");
        choice = scn.next();
        if (choice.equalsIgnoreCase("Y"))
        {

        System.out.print("Enter employee number: ");
        int number = scn.nextInt();

        while (!one.findEmpNumber(number))
        {
            System.out.print("Invlaid, enter a proper employee number: ");
            number = scn.nextInt();
        }

        System.out.print("Enter first name: ");
        firstName = scn.next();

        System.out.print("Enter last name: ");
        lastName = scn.next();

        System.out.print("Enter hours worked: ");
        hoursWorked = scn.nextDouble();

        while (hoursWorked < 0)
        {
            System.out.print("Negative hours not allowed. Enter hours worked: ");
            hoursWorked = scn.nextDouble();
        }

        System.out.print("Enter hourly wage: $");
        hourlyWage = scn.nextDouble();

        while (hourlyWage < 0 || hourlyWage > 100)
        {
            System.out.print("Negative wage is not allowed or wage entered is to high. Enter hourley wage: $");
            hourlyWage = scn.nextDouble();
        }
        double overtimeHours = hoursWorked - 40; 
        double overtimeWage = hourlyWage * 1.5;
        System.out.println(" ");

        if (hoursWorked <= 40.0)
        {
        System.out.println("Worker " + number + " Paycheck Information: ");
        System.out.println("Name is: " + firstName + " " + lastName);
        System.out.println("Weekly Pay is: " + one.callWeeklyPay(hoursWorked, hourlyWage));
        System.out.println("Income Taxes is: " + one.callIncomeTax());
        System.out.println("Net Pay is: " + one.callNetPay());
        }
        else if (hoursWorked > 40.0)
        {
        System.out.println("Worker " + number + " Paycheck Information: ");
        System.out.println("Name is: " + firstName + " " + lastName);
        System.out.println("Weekly Pay is: " + one.callWeeklyPay(hoursWorked, hourlyWage));
        System.out.println("Income Taxes is: " + one.callIncomeTax());
        System.out.println("Net Pay is: " + one.callNetPay());

        System.out.println(" ");

        System.out.println("Worker " + number + " Overtime Calculation: ");
        System.out.println("Overtime Pay is: " + one.callOvertimePay(overtimeHours, overtimeWage, hourlyWage, hoursWorked));
        System.out.println("Overtime Income Tax is: " + one.callOvertimeTax());
        System.out.println("Overtime Net Pay is: " + one.callOvertimeNetPay());
        System.out.println("Total Net Pay is: " + one.callTotalNetPay());
        System.out.println(" ");
        }
        }
        else if (!choice.equalsIgnoreCase("Y"))
            {
                processAnotherEmployee = false;
                System.out.println("Total number of Employees processed: ");
                System.out.println(" ");
                System.out.println("End of program");
                break;
            }
            }
        }
    }
} 

package paytime;

public class Employee {
    private int empNumbers [] = {101, 103, 106, 109, 110, 113, 116, 118, 120};
    public double weeklyPay, hoursWorked, hourlyWage, incomeTax, netPay, 
            overtimePay, overtimeHours, overtimeWage, overtimeIncomeTax, 
            overtimeNetPay, totalNetPay;

    public boolean findEmpNumber(int number)
    {
        boolean found = false;
        for (int sub = 0; sub < empNumbers.length; sub++)
        {
            if (number == empNumbers[sub])
            {
                found = true;
                break;
            }
        }
        return found;
    }

    private void calculateWeeklyPay(double hoursWorked, double hourlyWage) {
        if (hoursWorked > 40)
        {
            hoursWorked = 40;
            weeklyPay = hoursWorked * hourlyWage;
        }
        else
        {
            weeklyPay = hoursWorked * hourlyWage;
        }
    }

    public double callWeeklyPay(double hoursWorked, double hourlyWage) {
        calculateWeeklyPay(hoursWorked, hourlyWage);
        return weeklyPay;
    }

    private void calculateIncomeTax() {
        if (weeklyPay > 0.0 && weeklyPay <= 300.0)
        {
            incomeTax = weeklyPay * 0.10;
        }
        else if (weeklyPay > 300.1 && weeklyPay <= 400.0)
        {
            incomeTax = weeklyPay * 0.12;
        }
        else if (weeklyPay > 400.1 && weeklyPay <= 500.0)
        {
            incomeTax = weeklyPay * 0.15;
        }
        else if (weeklyPay > 500.1)
        {
            incomeTax = weeklyPay * 0.20;
        }
    }

    public double callIncomeTax() {
        calculateIncomeTax();
        return incomeTax;
    }

    private void calculateNetPay() {
        netPay = weeklyPay - incomeTax;
    }

    public double callNetPay() {
        calculateNetPay();
        return netPay;
    }

    private void calculateOvertimePay(double overtimeHours, double overtimeWage, double hourlyWage, double hoursWorked) {
        overtimePay = overtimeHours * overtimeWage;
    }

    public double callOvertimePay(double overtimeHours, double overtimeWage, double hourlyWage, double hoursWorked) {
        calculateOvertimePay(overtimeHours, overtimeWage, hourlyWage, hoursWorked);
        return overtimePay;
    }

    private void calculateOvertimeTax() {
        overtimeIncomeTax = overtimePay * 0.25;
    }

    public double callOvertimeTax() {
        calculateOvertimeTax();
        return overtimeIncomeTax;
    }

    private void calculateOvertimeNetPay() {
        overtimeNetPay = overtimePay - overtimeIncomeTax;
    }

    public double callOvertimeNetPay() {
        calculateOvertimeNetPay();
        return overtimeNetPay;
    }

    private void calculateTotalNetPay() {
        totalNetPay = netPay + overtimeNetPay;
    }

    public double callTotalNetPay() {
        calculateTotalNetPay();
        return totalNetPay;
    }
}

1 个答案:

答案 0 :(得分:1)

您可以通过简单地使“ int employeeProcessed = 0;”来实现。在while循环之外,然后添加“ employeesProcessed ++;”直接在“ if(cho(equice.equalsIgnoreCase(“ Y”)))”之后,因此每次要求您的程序处理雇员时,您都要向int加1,以跟踪您处理了多少名员工。然后,您可以将此变量添加到打印字符串的末尾,使其显示“已处理的员工总数:” + employeeProcessed。