gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:28:00 25 4
gpt4 key购买 nike

我一直在为我的类(class)开发一个项目。这是一个简单的工资计算器,我很容易就学会了,而且功能完美。它还考虑了佣金基准。不过,我已经完成了最后一部分的切割。我无法让它发挥作用。它要么无限循环,要么在“总薪酬”列下生成静态数字(它们应该根据佣金等动态)。

  • 鲍勃每年赚取 85,000 美元。
  • 如果他的销售额低于 120,000 美元,他就不会获得奖金/佣金
  • 如果他当年的销售额 >= 120,000 美元,他将获得 15% 的佣金
  • 每销售额 15 万美元,他可获得 2% 的额外提成(15 万、30 万、45 万)等)

用户输入销售数字(例如: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);

}

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

最佳答案

此代码将迭代直到表达式 (PotentialSales < AcceleratorGoal)false 。由于您没有更新 PotentialSales 的值& AcceleratorGoaldo{..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);

您可能想要更新 PotentialSalesAcceleratorGoal do while 循环体内的值或更改条件 w.r.t output变量。

接下来的两个do..while也是如此循环。

关于Java - For 循环(do/while)无限重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31901165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com