I have completed this program and it runs successfully; however, as part as an a
ID: 3544115 • Letter: I
Question
I have completed this program and it runs successfully; however, as part as an assignment I need to add this to it:
When it is working properly, add a while or do while loop to allow the user to run the CD calculation multiple times, entering a 0 initial deposit amount to exit. Change the prompt for the initial deposit amount to tell the user to enter 0 to exit.
I'm pretty lost at this point. I tried adding while and do-while, but I can't get either to work. Any help would be greatly appreciated. Thanks!
import java.util.Scanner;
public class CDvalue
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the initial deposit amount: ");
double deposit = input.nextDouble();
System.out.print("Enter annual percentage yield: ");
double annualInterest = input.nextDouble();
double monthlyInterest = (annualInterest / 1200.0);
System.out.print("Enter the maturity period (number of months): ");
int months = input.nextInt();
System.out.print(" Month CD Value ");
for(int i = 1; i <= months; i++)
{
deposit *= (1.0 + monthlyInterest);
System.out.printf("%5d%20.2f ", i, deposit);
}
}
}
Explanation / Answer
import java.util.Scanner;
public class CDvalue
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while(true)
{
System.out.print("Enter the initial deposit amount: (0 to exit):");
double deposit = input.nextDouble();
if((int)deposit==0) break;
System.out.print("Enter annual percentage yield: ");
double annualInterest = input.nextDouble();
double monthlyInterest = (annualInterest / 1200.0);
System.out.print("Enter the maturity period (number of months): ");
int months = input.nextInt();
System.out.print(" Month CD Value ");
for(int i = 1; i <= months; i++)
{
deposit *= (1.0 + monthlyInterest);
System.out.printf("%5d%20.2f ", i, deposit);
}
} //end while.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.