Java - for循环(do / while)无限重复

时间:2015-08-09 05:20:49

标签: java loops if-statement while-loop for-else

我一直在为我的课程开展一个项目。这是一个简单的薪水计算器,我很容易理解,它功能完美无缺。它还考虑了佣金的基准。不过,我已经完成了最后一部分。我无法让它发挥作用。它要么无限循环,要么在“总薪酬”列下生成静态数字(它们应该是基于佣金的动态等)。

  • 鲍勃每年赚85,000美元。
  • 如果他的售价不到120,000美元
  • ,他就没有奖金/佣金
  • 如果他今年的销售额> = 120,000美元
  • ,他将获得15%的佣金
  • 每售价15万美元,他的额外收入增加2%(150k,300k,450k 等)

用户输入销售号码(例如:$ 300,000)。 程序计算BaseSalary +($ 300,000 * .15)+($ 300,000 * .02)

左侧的潜在销售额,然后是右侧的总薪酬

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);

            }          

任何建议或提示都将成为救星。

2 个答案:

答案 0 :(得分:2)

此代码将迭代,直到表达式(PotentialSales < AcceleratorGoal)false。由于您没有更新PotentialSales&amp;的值在AcceleratorGoal的正文中do{..body..}while(expression),如果(PotentialSales < AcceleratorGoal)true处于while,它将无限期地迭代,否则它将被执行一次并且将在下一个语句之后转到下一个语句do{ String output = numberFormat.format(PotentialSales) + "\t\t" + numberFormat.format(BaseSalary); System.out.println(output); }while(PotentialSales < AcceleratorGoal); 条件。

<强>代码

PotentialSales

您可能希望更新do while循环体内的AcceleratorGoaloutput值,或更改条件w.r.t do..while变量。

接下来的两个datatoggle="collapse"循环也是如此。

答案 1 :(得分:0)

你的do while循环无限重复,因为你没有在while循环中更新PotentialSales的值

要了解此问题,请检查以下代码

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

运行此示例代码,您将了解问题。