作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在为我的类(class)开发一个项目。这是一个简单的工资计算器,我很容易就学会了,而且功能完美。它还考虑了佣金基准。不过,我已经完成了最后一部分的切割。我无法让它发挥作用。它要么无限循环,要么在“总薪酬”列下生成静态数字(它们应该根据佣金等动态)。
用户输入销售数字(例如: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
的值& 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
或AcceleratorGoal
do while 循环体内的值或更改条件 w.r.t output
变量。
接下来的两个do..while
也是如此循环。
关于Java - For 循环(do/while)无限重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31901165/
我是一名优秀的程序员,十分优秀!