嘿,我是编码的新手,我想知道你们是否可以帮助我计算不同的利率,然后将其添加到下一个利率中。所以基本上我想尝试获得利率A并将其添加到初始值100。然后我想获得100的利率B并将该值添加到利率A。到目前为止,这是我的代码,但我得到10行每个利率。抱歉,这听起来令人困惑,但希望代码能使它更清晰,或者,如果有人想读这本书,我可能会尝试更好地解释。谢谢!!
int intv;
cout << " Accumulate interest on a savings account. ";
cout << " Starting value is $100 and interest rate is 1.25% ";
cout << endl;
intv = 100;
index = 1;
while ( index <= 10 )
{
cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.27% for a total of " << .0127 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.28% for a total of " << .0128 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.30% for a total of " << .0130 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.31% for a total of " << .0131 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.32% for a total of " << .0132 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.35% for a total of " << .0135 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.36% for a total of " << .0136 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.38% for a total of " << .0138 * intv + intv << "." << endl;
cout << " Year " << index << " adds 1.40% for a total of " << .0140 * intv + intv << "." << endl;
index = index + 1;
}
与其为我做这件事,我只想提示。我想自己修复此问题,但对我必须做的事情感到困惑。
期望的结果是程序给我这个:
1年级加1.25总计101.25
2年级加1.27,总计102.52
3年级加1.28,总计103.80
四年级加1.30,总计105.09
5年级加1.31,总计106.41
6年级加1.33,总计107.74
7年级加1.35,总计109.09
八年级加1.36,总计110.45
9年级加1.38,总计111.83
10年级加1.40,总计113.23
计入的利息总额为13.23
最佳答案
听起来您可以使用for
循环:
double rate = 0.125;
for (unsigned int index = 0; index < max_rates; ++index)
{
cout << " Year " << index << " adds "
<< (rate * 100.0)
<< "% for a total of "
<< rate * intv + intv << "." << endl;
rate += 0.002;
}