In: Computer Science
Create a Java Program to show a savings account balance. using eclipse IDE
This can be done in the main() method.
Enter amount to save
each month:35
Enter the number of months to save:12
For month 1, the balance is 35
For month 2, the balance is 70
For month 3, the balance is 105
For month 4, the balance is 140
For month 5, the balance is 175
For month 6, the balance is 210
For month 7, the balance is 245
For month 8, the balance is 280
For month 9, the balance is 315
For month 10, the balance is 350
For month 11, the balance is 385
For month 12, the balance is 420
Below is your code:
import java.util.Scanner;
public class SavingsAccountDriver {
public static void main(String[] args) {
// initializing Scanner object to get entries from user
Scanner sc = new Scanner(System.in);
// Create an int variable named currentBalance and assign it the value
// of 0.
int currentBalance = 0;
// Create an int variable named amountToSaveEachMonth.
int amountToSaveEachMonth;
// Prompt "Enter amount to save each month:" and assign the result to
// the int variable in step 2.
System.out.print("Enter amount to save each month:");
amountToSaveEachMonth = Integer.parseInt(sc.nextLine());
// Create an int variable name numberOfMonthsToSave.
int numberOfMonthsToSave;
// Prompt "Enter the number of months to save:" and store the input
// value into the variable in step 4.
System.out.print("Enter the number of months to save:");
numberOfMonthsToSave = Integer.parseInt(sc.nextLine());
// Create an int variable named currentSavingsMonth and assign it the
// value of 1.
int currentSavingsMonth = 1;
// Using a while loop, do the following while the currentSavingsMonth <=
// numberOfMonthsToSave
while (currentSavingsMonth <= numberOfMonthsToSave) {
// Add the amountToSaveEachMonth to the current balance.
currentBalance = currentBalance + amountToSaveEachMonth;
// Display the currentSavingsMonth and the current balance
System.out.println("For month " + currentSavingsMonth
+ ", the balance is " + currentBalance);
// Add 1 to the currentSavingsMonth
currentSavingsMonth++;
}
}
}
Output