0

I have the following code :

double getTotalPayments(){
    for (int i = 0 ; i == periodsPerYear * years ; i++){
      double balance =+ Math.round(initialBalance - (monthlyPayment-((interestRate/periodsPerYear) *initialBalance))*100.00)/100.00;
      initialBalance =- balance;
      if(i == periodsPerYear*years){
        return balance;
        break;
      }
    }
    return balance;
  }

I'm trying to pass the double variable 'balance' for the method to return. I have to use the for loop to calculate the total amount of payments. Any suggestions on how to fix it? I've tired everything I can think of.

1
  • Your end condition in your for loop should be 'i < periodPerYear * years' Commented Feb 24, 2020 at 7:24

3 Answers 3

1

I think you're just missing a few things within the code. It would probably go something like this. You were just missing the bits within the method declaring that it's either a public or private method, and that you weren't calling in the Int Variable balance.

public double getTotalPayments(int balance){
for (int i = 0 ; i == periodsPerYear * years ; i++){
  double balance =+ Math.round(initialBalance - (monthlyPayment-((interestRate/periodsPerYear) *initialBalance))*100.00)/100.00;
  initialBalance =- balance;
  if(i == periodsPerYear*years){
    return balance;
    break;
  }
}
return balance;

Please let me know if that works!

Sign up to request clarification or add additional context in comments.

Comments

0

It should be

double balance += Math.round(initialBalance - 
(monthlyPayment-((interestRate/periodsPerYear) 
 *initialBalance))*100.00)/100.00;

initialBalance -= balance;

Comments

0

How can you return balance when "balance" is only a local variable in for loop? And another like @NomadMaker said that the condition for the for loop should be "i < periodPerYear * years".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.