我必须使这个GUI成为CD计算器。
I put in the Initial investment ($): e.g. 2000
the Annual interest rate (%): e.g. (8%)
Ending value ($): e.g. 5000
程序然后在jLabel上输出:所需的年数为“12”(例如)
我需要做一个while循环和一个计数器。
我从3个文本字段中获取了获取文本,然后使用年费率%添加initialInvestment,但是在循环和计数器方面遇到了问题?
int initialInvestment, endValue, cdvalue;
double cdValue, annualDecimalConvert, annualRate;
initialInvestment = Integer.parseInt(initialInvestmentInput.getText());
annualRate = Double.parseDouble(interestRateInput.getText())/100;
endValue = Integer.parseInt(endingValueInput.getText());
cdValue = initialInvestment + (initialInvestment * annualRate);
double a = cdValue;
while (a <= endValue){
a = a++;
yearsOutput.setText("The required year needed is: " + a);
}
答案 0 :(得分:3)
您只需在循环的每次迭代中向a
添加1。因此,需要几千次迭代才能满足循环要求。
你要做的就是每年不断增加兴趣,同时保持年份的数量,只有在你完成循环后才更新输出。
int initialInvestment, endValue;
double cdValue, annualDecimalConvert, annualRate;
initialInvestment = Integer.parseInt(initialInvestmentInput.getText());
annualRate = Double.parseDouble(interestRateInput.getText())/100;
endValue = Integer.parseInt(endingValueInput.getText());
// First year interest is counted here.
cdValue = initialInvestment + (initialInvestment * annualRate);
int years = 1;
while (cdValue < endValue){
cdValue = cdValue + (cdValue * annualRate);
years++;
}
yearsOutput.setText("The required year needed is: " + years);