java loop to repeat program

前端 未结 3 353
余生分开走
余生分开走 2021-01-28 07:17

I am extremely new to java I am in my second week in classes or so--

I need my program to keep going or exit according to the user. It is a payroll calculation and I wan

相关标签:
3条回答
  • 2021-01-28 07:25

    Here is a brief idea of how to do this:

    String choice = "y";
    
    while(true) {
        //Take input
        System.out.print("Do you want to continue (y/n)?");
        choice = reader.nextLine();
    
        if(choice.equalsIgnoreCase("n")) break;
        //else do your work.
    }
    

    You can also sue do-while to get what you want. You may need to make some changes but then that is the entire idea. It is just a hint.

    0 讨论(0)
  • 2021-01-28 07:36

    Here is the basic idea with your code:

    import java.util.Scanner;
    
    public class calculations{
        public static void main(String [] args) {
            Scanner reader = new Scanner(System.in);
            Scanner in = new Scanner(System.in);
    
    
            double Regpay;
            double Payperhour;
            int HoursAweek;
            double Pay;
            double OvertimeHours;
            double OvertimePay;
            double Dependants;
            double SocSecTax;
            double FederalTax;
            double StateTax;
            int UnionDues;
            double AllTaxes;
            double FinalPay;
            String playAgain;
            int runAgain = 1;
    
            while (runAgain == 1) {
    
    
                System.out.print("Enter your pay per hour:");
                Payperhour = reader.nextDouble();
    
                System.out.print("Enter your regular Hours a week:");
                HoursAweek = reader.nextInt();
    
                System.out.print("Enter your overtime hours:");
                OvertimeHours = reader.nextDouble();
    
                Regpay = Payperhour * HoursAweek;
    
                OvertimePay = OvertimeHours * 1.5 * Payperhour;
    
                Pay = OvertimePay + Regpay;
    
                SocSecTax = Pay * .06;
    
                FederalTax = Pay * .14;
    
                StateTax = Pay * .05;
    
                UnionDues = 10;
    
                AllTaxes = SocSecTax + FederalTax + StateTax + UnionDues;
    
                FinalPay = Pay -= AllTaxes;
    
                System.out.println("Your pay this week will be " + FinalPay);
                {
    
    
                    System.out.println("How many Dependants:");
                    Dependants = reader.nextInt();
    
                    if (Dependants >= 3) {
                        Dependants = Pay + 35;
                        System.out.println("Your Pay is:" + Dependants);
                    } else if (Dependants < 3) {
    
                        System.out.println("Your Pay is:" + Pay);
                    }
    
                }
    
                System.out.println("Again???  Press 1 to run again and 0 to exit");
                runAgain = reader.nextInt();
    
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-28 07:46

    Here's a guide for you..

    You can create a method for the transaction.

    //Place this on your main method
    do{
       //call the method
        transaction();  
       //ask if the user wants to repeat the program
       System.out.print("Do you want to continue (y/n)");
       input = reader.nextLine(); 
    }while(input.equalsIgnoreCase("Y"))
    

    public void transaction(){
       //your transaction code here.. 
    }
    
    0 讨论(0)
提交回复
热议问题