The solution works, but the iteration keeps looping to month 1 within the for lo
ID: 3533672 • Letter: T
Question
The solution works, but the iteration keeps looping to month 1 within the for loop. I need to find out how to get the following Output:
Enter the savings account's balance: 10000
Enter the savings account's annual interest rate: .02
How many months have passed since the account was opened? 3
Enter the amount deposited during month 1: 1200
Enter the amount withdrawn during month 1: 900
Enter the amount deposited during month 2: 1500
Enter the amount withdrawn during month 2: 1100
Enter the amount deposited during month 3: 2000
Enter the amount withdrawn during month 3: 1800
Total Deposited: $4,700.00 Total withdrawn: $3,800.00
Interest Earned: $53.25
Ending Balance: $10,953.25
What I need help with is having the program iterate 3 times because the user entered 3 for how many months the account has been opened. And the set precision for dollar amounts. This is an intro to Java class so simple code is needed here, I wouldn't understand any advanced code. Thank You!
Explanation / Answer
import java.util.Scanner;
public class Savings {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
double balance;
double rate;
int month;
double totalDeposit=0;
double interestEarned=0;
double endingBalance;
double totalWithdrawn=0;
System.out.println("Enter the savings account's balance");
balance=Double.parseDouble(scan.nextLine());
System.out.println("Enter the savings account's annual interest rate");
rate=Double.parseDouble(scan.nextLine());
System.out.println("How many months have passed since the account was opened?");
month=Integer.parseInt(scan.nextLine());
for(int i=1;i<=month;i++)
{
System.out.println("Enter the amount deposited during month "+i);
totalDeposit=totalDeposit+Double.parseDouble(scan.nextLine());
System.out.println("Enter the amount withdrawn during month "+i);
totalWithdrawn=totalWithdrawn-Double.parseDouble(scan.nextLine());
}
System.out.println("total deposit :" + totalDeposit);
System.out.println("total withdrawn :" + totalWithdrawn);
System.out.println("closing balance :"+(balance+totalDeposit-totalWithdrawn));
}
}