PDA

View Full Version : Programming loops

jerelp
07-13-2011, 02:44 AM
Hello,

I just want to know if I'm on the right track with this program. I can't seem to grasp if i'm using the correct loop or not.

The assignment is:

Bank Balance **:

The First National Bank of Parkville recently opened up a new “So You Want to Be a Millionaire” savings account. The new account works as follows:

• The bank doubles the customer’s balance every year until the customer’s balance reaches one million.
• The customer isn’t allowed to touch the money (no deposits or withdrawals) until the customer’s balance reaches one million.
• If the customer dies before becoming a millionaire, the bank keeps the customer’s balance.
• Note: Customers close to \$1,000,000 tend to get “accidentally” run over in the bank’s parking lot.

Write a program that prompts the user for a starting balance and then prints the number of years it takes to reach \$100,000 and also the number of years it takes to reach \$1,000,000.

Sample session:

Enter starting balance: 10000
It takes 4 years to reach \$100,000.
It takes 7 years to reach \$1,000,000.

This is the code I have so far. I'm only doing for 100,000 and seeing if that works first before I move on to the next 1,000,000. any help would be great. Thank you for your time.

/****************************************************
* Bank.java
* Jerel Paule
* 07-06-2011
* This Program Calculates the area, diameter, and
****************************************************/

import java.util.Scanner;

public class Bank
{
public static void main(String[] args)
{

Scanner stdIn = new Scanner(System.in);
//Variables
int balance; // starting balance
int i = 0;
// int j = 0;

do
{

balance = stdIn.nextInt();

i ++;

if (balance < 1000000)
{
balance = balance * 2;

}
} while (balance <= 100000);

System.out.println("It takes "+ i + " years to reach \$100,000.");
}
}

Old Pedant
07-13-2011, 03:12 AM
This is the JavaSCRIPT forum.

That code is written in JAVA.

About the only thing the two languages have in common are the first four letters of their names.

***********

Having said that:

Your DO loop will have the user entering a starting balance *EVERY YEAR*. The starting balance needs to be entered *ONCE*, before the DO loop.

I think you need to (a) scrap that code (including the entirely irrelevant comments in the first few lines and (b) try again, but post in the JAVA forum.

bullant
07-13-2011, 11:02 AM
Your DO loop will have the user entering a starting balance *EVERY YEAR*.

I don't think that is correct.

The DO part of a DO-WHILE loop is executed only once.

Then inside the WHILE part, just increment a counter representing the years and double the balance. Don't print anything.

When the WHILE's test condition is false, print the value of the counter (years) just below the WHILE part.

But I would use the DO-WHILE to make sure the user enters a balance below 1000000 and then use another while loop to do the doubling of the balance and counting of iterations while the balance is < 1000000.