简体   繁体   中英

Java - For loop (do/while) repeats indefinitely

I have been working on a project for my course. It's a simple salary calculator, which I have figured out fairly easily and it functions flawlessly. It also takes into consideration benchmarks for commission. I have completed choped on the last part, however. I cannot get it to function. It either loops indefinitely, or it generates static numbers under the "Total Compensation" column (they should be dynamic based on commission etc).

  • Bob makes $85,000 a year.
  • He gets no bonus/commission if he sells less than $120,000
  • He gets 15% commission if his sales for the year are >= $120,000
  • He gets an additional 2% for every $150k in sales (150k, 300k, 450k etc)

User inputs a sales number (example: $300,000). Program calculates BaseSalary + ($300,000 * .15) + ($300,000 * .02)

Potential Sales on the left and then Total Compensation on the right side

200,000 ----------------------> (Calculated)

220,000 ----------------------> (Calculated)

240,000 ----------------------> (Calculated)

260,000 ----------------------> (Calculated)

280,000 ----------------------> (Calculated)

300,000 ----------------------> (Calculated)

//Variables defined here
        double Sales = input.nextDouble();
        double TotalCompensation;
        int Commission;
        double CommissionRate;
        double AcceleratedCommissionRate;
        int BaseSalary;
        double TargetSales;
        double Accelerator;
        double AcceleratorGoal;
        double AcceleratedCommission;

// Mathematical Operations
        Accelerator = 0.80;
        TargetSales = 150000;
        CommissionRate = 0.15;
        AcceleratedCommissionRate = ((CommissionRate) * (2.0));
        BaseSalary = 85000;
        Commission = (int) (Sales * CommissionRate);
        AcceleratedCommission = (double) (Sales * AcceleratedCommissionRate);
        AcceleratorGoal = (double) 120000; 


// If Else Logic Flow

        if(Sales < AcceleratorGoal)
        {
            TotalCompensation = BaseSalary;
            System.out.print("\nCommission Goal HAS NOT been met");
            System.out.println("\nTotal annual compensaton is: ");
            System.out.println(numberFormat.format(TotalCompensation));

        }

        else if(Sales < TargetSales)
        {
            TotalCompensation = BaseSalary + Commission;
            System.out.print("\nCommission Goal HAS been met");
            System.out.println("\nTotal annual compensaton is: ");
            System.out.println(numberFormat.format(TotalCompensation));
        }

        else if(Sales >= TargetSales)
        {
            TotalCompensation = BaseSalary + AcceleratedCommission;
            System.out.print("\nAccelerator Goal HAS been met");
            System.out.println("\nTotal annual compensaton is: ");
            System.out.println(numberFormat.format(TotalCompensation));

        }

// Potential Commission Structure Table

        double PotentialSales;
        double EndLoop = (Sales * 1.5);
        int Increment = 20000;

            System.out.println("\nPotential Sales\t\tTotal Compensation");

            for (PotentialSales = Sales; PotentialSales < EndLoop; 
            PotentialSales = PotentialSales += Increment)   
            {

                do
                {
                    String output = numberFormat.format(PotentialSales) + "\t\t" 
                    + numberFormat.format(BaseSalary);

                    System.out.println(output);
                }   
                    while(PotentialSales < AcceleratorGoal);

                do
                {
                    String output = numberFormat.format(PotentialSales) + "\t\t" 
                    + numberFormat.format(Commission + BaseSalary);

                    System.out.println(output);
                }
                    while(PotentialSales >= AcceleratorGoal && PotentialSales < TargetSales);

                do
                {
                    String output = numberFormat.format(PotentialSales) + "\t\t" 
                    + numberFormat.format(AcceleratedCommission + BaseSalary);

                    System.out.println(output);
                }
                    while(PotentialSales >= TargetSales);

            }          

Any suggestions or tips whatsoever would be a lifesaver.

This code will iterate until expression (PotentialSales < AcceleratorGoal) is false . As you are not updating the value of PotentialSales & AcceleratorGoal in the body of do{..body..}while(expression) , it will iterate indefinitely if (PotentialSales < AcceleratorGoal) is true at first place, otherwise it will get executed once and will go to the next statement after the while condition.

Code

do{
    String output = numberFormat.format(PotentialSales) + "\t\t" + numberFormat.format(BaseSalary);                   
    System.out.println(output);
}while(PotentialSales < AcceleratorGoal);

You might want to update the PotentialSales or AcceleratorGoal Value inside the body of do while loop or change condition wrt output variable.

Same goes for next two do..while loops.

Your do while loop repeats infinitely because,you are not updating the value of PotentialSales in do while loop

To understand the problem,check the following code

int accelerationsales=5;
for(int potentialsales=0;potentialsales<5;potentialsales=potentialsales+=1) {
do {
    System.out.println("Hello");
   } while(potentialsales<accelerationsales);
}

Run this sample code,you will understand the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM