**这是我的代码,我希望在每次迭代时改变in-value(它应该减少,因为它是一个系列贷款).我在MacOS上的Xcode中运行它.**
void calculateSeries(){
int loan;
cout<<"Total loan as of today:\n";
cin>> loan;
int series;
cout<<"Number of series\n";
cin>>series;
int interest;
cout<<"Interest:\n";
cin>>interest;
//vector<int> loan_vector(series);
for (int i=1; i<=series; i++){
double in=(loan/series)+(interest/100)*(loan-(loan/series)*i);
//cout<<in<<"\n";
//loan_vector.push_back(in);
cout<<" Payment year " << i <<" " << in << "\n";}
}
Run Code Online (Sandbox Code Playgroud)
我的输出是这样的:
Total loan as of today:
10000
Number of series
10
Interest:
3
Payment year 1 1000
Payment year 2 1000
Payment year 3 1000
Payment year 4 1000
Payment year 5 1000
Payment year 6 1000
Payment year 7 1000
Payment year 8 1000
Payment year 9 1000
Payment year 10 1000
Run Code Online (Sandbox Code Playgroud)
您的表达(interest/100)与interest感型的int是一个整数除法和-一次的值interest是<100,将总是导致0,由于结果的任何小数部分都将被丢弃(参见,例如,该在线C++标准草案):
5.6乘法运算符
- ...对于积分操作数,/运算符产生代数商,丢弃任何小数部分
因此,术语(interest/100)*(loan-(loan/series)*i)也将使0您的结果(loan/series)+0在每次迭代中都是如此.
写(interest/100.)(注意.在100.使第二个参数的浮点值),使得该术语将是一个浮点除法(而不是一个整数除法).
BTW:loan并且interest应该有类型double而不是int反正.