加班计算器,创建循环的问题

时间:2016-03-08 14:59:16

标签: java

我设法让我的计算器正常工作,但我试图引入一个循环以允许用户运行第二次搜索。我正在尝试使用Char(0)进行Y / N回答,但任何其他方式使其工作都会很棒

import java.util.*;

public class Overtime
{
    public static void main ()
    {
        Scanner inputLine = new Scanner(System.in);

        String staffName;

        double hoursWorked = 0, hourlyRate = 15, totalPay=0, nationalInsurance=0, tax=0, netPay=0, overtime=0;


        do{
        System.out.print ("Enter employee name: ");

        staffName = inputLine.nextLine();

        System.out.print ("Enter number of hours worked: ");

        hoursWorked = inputLine.nextFloat();

        if ( hoursWorked <= 36 ) 
        {
            totalPay = (hourlyRate * hoursWorked);
        } 
        else if ( hoursWorked >= 37 && hoursWorked <= 40)
        {
            totalPay = (hourlyRate * 36) + (hoursWorked - 36) * (hourlyRate * 1.5);
        } 
        else  //(hoursWorked > 41 ) 
        {
            totalPay = (hourlyRate * 36) + (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2);
        }

        if ( hoursWorked >= 37 && hoursWorked <= 40)
        {
            overtime = (hoursWorked - 36) * (hourlyRate * 1.5);
        }
        else if ( hoursWorked > 41)
        {
            overtime = (41-36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate *2);
        }

        if (totalPay > 155 )
        {
            nationalInsurance = (totalPay * 0.12);
        }


        tax = (totalPay * 0.20);
        netPay = (totalPay - tax - nationalInsurance);

        if (hoursWorked >= 49 )
        {
         System.out.println ("You are not legally allowed to work over 48 hours! Please enter correct hours: ");
        } 

        else 
        System.out.println ( "***********************");
        System.out.println ("Employee: " + staffName);
        System.out.println ("Total Hours Worked: " + hoursWorked);
        System.out.println ("Overtime Pay: " + overtime);
        System.out.println ("Net Pay: " + totalPay); 
        System.out.println ("Tax: " + tax);
        System.out.println ("National insurance: " + (nationalInsurance = Math.round(nationalInsurance * 100.00 )/ 100.00));
        System.out.println ("Net Pay" + netPay);
        //System.out.println ("Overtime Pay" + (hoursWorked - 36) * (hourlyRate * 1.5) + (hoursWorked - 41) * (hourlyRate * 2));
        //System.out.println ("Total Deductions " + (totalPay * (20/100) + (totalPay * (4/100))));
    }
   }
    System.out.println ("Would you like to try another user");

}

1 个答案:

答案 0 :(得分:2)

您只需将逻辑括在do while循环中,这是一个示例:

boolean exit = false;
do{
    //your code
    System.out.println("Do you want to add more?");
    String choice = sc.next();
    if("N".equalsIgnoreCase(choice)){
        exit = true;
    }
}while(!exit);