-3

I am trying to run balN with the variable N changing as I change it in the program but balN is only running it with the value that was assigned with N at the very beginning. How can I make it so balN will change as the value of N changes? I am new to programming so any help will be appreciated. Thanks!

const double Monintrate = 0.09 / 12.0;
const double Totnum = 36.0;
const double Payment = 165.25;

int main ()
{
  double N = 1.0;
  double balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;

  cout << fixed;
  cout << setprecision(2) << "Monthly Payment: $" <<  Payment << endl;
  cout << "Annual Interest rate: 9%" << endl;
  cout << "Total Number of Payments: " << Totnum << endl << endl;

  cout << "Balance after Payment:" << endl;
  cout << "1: $" << balN << endl;
  N++;
  cout << "2: $" << balN << endl;
  N++;
  cout << "3: $" << balN << endl;
}
4

4 回答 4

4

因为在您的程序中,您只修改了 的值N,而不是balN,您需要调用

balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;

每次修改N.

cout << "1: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "2: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "3: $" << balN << endl;
于 2013-09-11T13:55:36.583 回答
2

您永远不会balN根据N.

每次更改时都应该重新分配它N

cout << "1: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "2: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "3: $" << balN << endl;

也许这里的循环可以成为避免代码重复的解决方案:

for ( int i = 1; i <= 3; i++ )
{
    balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
    cout << i << ": $" << balN << endl;
    N++;
}

或者使用一个函数来做到这一点:

double calcBalN( double N )
{
    return Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
}

int main ()
{
    // ...
    cout << "1: $" << calcBalN( N ) << endl;
    N++;
    cout << "2: $" << calcBalN( N ) << endl;
    N++;
    cout << "3: $" << calcBalN( N ) << endl;
    //...
}
于 2013-09-11T13:57:26.347 回答
1
double balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;

赋值右侧的表达式将被评估,结果将存储在balN. 如果您不为其重新分配新值,balN则不会修改 的值,即使N修改了 的值也是如此。

你可以做的是在一个函数中定义你的计算并balN用它更新:

double process(double N) {
    const double Monintrate = 0.09 / 12.0;
    const double Totnum = 36.0;
    const double Payment = 165.25;
    return Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;;
}

int main ()
{
    double N = 1.0;
    double balN = process(N);

    cout << fixed;
    cout << setprecision(2) << "Monthly Payment: $" <<  Payment << endl;
    cout << "Annual Interest rate: 9%" << endl;
    cout << "Total Number of Payments: " << Totnum << endl << endl;

    cout << "Balance after Payment:" << endl;
    cout << "1: $" << balN << endl;
    N++;
    balN = process(N);
    cout << "2: $" << balN << endl;
    N++;
    balN = process(N);
    cout << "3: $" << balN << endl;

}
于 2013-09-11T13:59:27.253 回答
0

balN一个函数。C 语言不支持反应式编程,否则您可能会从您的代码中受益。以下代码将有所帮助。

const double Monintrate = 0.09 / 12.0;
const double Totnum = 36.0;
const double Payment = 165.25;

double balN (double N)·
{
double bal = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
return bal;·
}
int main ()
{
double N = 1.0;

cout << fixed;
cout << setprecision(2) << "Monthly Payment: $" <<  Payment << endl;
cout << "Annual Interest rate: 9%" << endl;
cout << "Total Number of Payments: " << Totnum << endl << endl;

cout << "Balance after Payment:" << endl;
cout << "1: $" << balN() << endl;
N++;
cout << "2: $" << balN() << endl;
N++;
cout << "3: $" << balN() << endl;
}
于 2013-09-11T13:59:00.547 回答