我需要代码来计算用户选择的值(每年节省的金额、达到的金额和利息百分比)。
我最好的猜测是这可以通过某种循环来解决吗?所以每年 500 * 1.05(利息是 5%)= 525 + 500 = 1025 * 1.05..等等。
可能没那么难...但我还需要知道达到目标需要多少年,例如需要循环多少次才能达到 >= 50 000。 我已经开始编写一些代码,但这是我继“Hello world”之后的第二件事。 任何对此的帮助都将是惊人的!
#include <iostream>
using namespace std;
int main() {
float save, goal, intrest;
cout << "save: ";
cin >> save;
cout << "goal: ";
cin >> goal;
cout << "intrest: ";
cin >> intrest;
float intrest1(ranta / 100 + 1);
float sum = save * intrest1;
while (sum < goal) {
sum + 500 =
}
cout << "summa: " << sum;
system("pause");
}
最佳答案
我认为您关于使用循环进行计算的说法是正确的,而且您也说这并不难 :)。
我只是稍微编辑了你的代码,让它输出你节省的总金额以及达到你的目标需要多少年。
每次迭代,我都会增加年数,然后将 sum
指定为那一年之后节省的金额。
希望这可以帮助您到达目的地。
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
float save, goal, interest;
cout << "save: ";
cin >> save;
cout << "goal: ";
cin >> goal;
cout << "intrest: ";
cin >> interest;
float interest1 = interest / 100 + 1;
float sum = 0;
int years = 0;
while (sum < goal) {
years = years + 1;
sum = sum * interest1 + save;
}
cout << "sum: " << sum << "\n";
cout << "years: " << years << "\n";
system("pause");
}
关于c++ - visual studio 2013 C++ 中的迭代循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613720/