如何更改for循环以便为我提供正确的解决方案?

时间:2014-08-05 18:45:55

标签: objective-c math for-loop

我正在尝试为抵押贷款摊销计划创建一个循环,但我在逻辑上遇到了一些问题 - 当将数值添加到数组中时,它每次都给我相同的值。是否有某种方法可以在每次循环运行时使我的余额值发生变化?

double balanceAmount = loanAmountValue;
    double rtemp = r / (n * 12);
    double intA = balanceAmount * rtemp;
    double principalA = payfinal - intA;
    double principal = balanceAmount - principalA;

    for (n = n * 12; n != 0; --n) {


        double realBalance = balanceAmount - principalA;
        double interest = balanceAmount * rtemp;
        NSLog(@"%f", interest);
        double principalAmount = payfinal - interest;
        NSLog(@"%f", principalAmount);

        NSLog(@"%f", realBalance);

        NSString *tempInterest = [NSString stringWithFormat:@"$%.2f", interest];
        [interestLabels addObject:tempInterest];
        NSString *tempPrincipal = [NSString stringWithFormat:@"$%.2f", principalAmount];
        [pricipalLabels addObject:tempPrincipal];
        NSString *tempBalance = [NSString stringWithFormat:@"$%.2f", realBalance];
        [balanceLabels addObject:tempBalance];

        NSLog(@"%@",pricipalLabels);
    }

1 个答案:

答案 0 :(得分:1)

您需要在循环结束时更改余额。尝试添加:balanceAmount -= principalA

for (n = n * 12; n != 0; --n) {
    // Your other code

    balanceAmount -= principalA
}