嘿,我是编码的新手,想知道你们是否可以帮助我计算不同的利率,然后将它们加到下一个利率。所以基本上我试图获得利率A并将其加到100的起始值。然后我想得到利率B为100并将该值加到利息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 第4年增加1.30,总计105.09 第5年增加1.31,总计106.41 第6年增加1.33,总计107.74 第7年增加1.35,总计109.09 第8年增加1.36,总计110.45 第9年增加1.38,总计111.83 第10年增加1.40,总计113.23
总利息为13.23
答案 0 :(得分:1)
听起来你可以使用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;
}
答案 1 :(得分:0)
您需要使用功能替换
cout << " Year " << index << " adds 1.25% for a total of " << .0125 * intv + intv << "." << endl;
该函数可以将索引转换为像
这样的值double foo(int index);
输入值为'index',输出值为1.25%,1.38%等增值值。
然后删除所有cout行。只需添加以下行:
cout << " Year " << index << " adds " << foo(index) * 100.0 << "% for a total of " << foo(index) * intv + intv << "." << endl;
我想这就是你想要的。