Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Programming loops

    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.

    Code:
    /****************************************************
    * Bank.java
    * Jerel Paule
    * 07-06-2011
    * This Program Calculates the area, diameter, and
    * Circumfrence from the radius
    ****************************************************/
    
    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
        {
    
          System.out.print("Please enter starting balance: ");
          balance = stdIn.nextInt();
    
          i ++;
    
          if (balance < 1000000)
          {
           balance = balance * 2;
    
          }
         } while (balance <= 100000);
    
         System.out.println("It takes "+ i + " years to reach $100,000.");
     }
     }

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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.

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by Old Pedant View Post
    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.
    Last edited by bullant; 07-13-2011 at 10:09 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •