gpt4 book ai didi

java - 初学者循环 GUI

转载 作者:行者123 更新时间:2023-12-01 23:25:16 25 4
gpt4 key购买 nike

我必须制作一个 CD 计算器的 GUI。

I put in the Initial investment ($): e.g. 2000
the Annual interest rate (%): e.g. (8%)
Ending value ($): e.g. 5000

程序然后在 jLabel 上输出:所需的年数为“12”(例如)

我需要制作一个 do while 循环和一个计数器。

我从 3 个文本字段中获取文本,然后添加带有年利率 % 的初始投资,但循环和计数器遇到问题?

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

最佳答案

您只需在循环的每次迭代中将 1 添加到 a 即可。因此,需要几千次迭代才能满足循环要求。

您要做的就是每年不断增加利息,同时计算年份,并且仅在循环完成后更新输出。

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

关于java - 初学者循环 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20103664/

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