简体   繁体   English

Java-For循环(执行/执行)无限期重复

[英]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. 鲍勃一年赚85,000美元。
  • He gets no bonus/commission if he sells less than $120,000 如果他的销售额低于$ 120,000,他将不会获得奖金/佣金
  • He gets 15% commission if his sales for the year are >= $120,000 如果他当年的销售额> = $ 120,000,他将获得15%的佣金
  • He gets an additional 2% for every $150k in sales (150k, 300k, 450k etc) 每15万美元的销售额(15万,30万,45万等),他将获得2%的额外回报

User inputs a sales number (example: $300,000). 用户输入销售编号(例如:300,000美元)。 Program calculates BaseSalary + ($300,000 * .15) + ($300,000 * .02) 程序计算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 . 此代码将迭代直到表达式(PotentialSales < AcceleratorGoal)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. 由于您没有在do{..body..}while(expression)的正文中更新PotentialSalesAcceleratorGoal的值,因此如果(PotentialSales < AcceleratorGoal)首先为true ,它将无限期地进行迭代,否则它将被执行一次并且在while条件之后将转到下一条语句。

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. 您可能想要更新do while循环主体内的PotentialSalesAcceleratorGoal值,或更改wrt output变量的条件。

Same goes for next two do..while loops. 接下来的两个do..while循环也是如此。

Your do while loop repeats infinitely because,you are not updating the value of PotentialSales in do while loop 您的do while循环无限重复,因为您没有在do while循环中更新PotentialSales的值

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. 运行此示例代码,您将了解问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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