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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Location
    Edmonton
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Writing a loop to determine total savings

    Hello,

    My girlfriend asked me to write a loop to determine the total amount she will save if she starts by putting in $75 and puts in 1 more dollar every week (75, 76, 77, ... 127).

    Right now, I'm stuck at trying to get the for loop to add the extra dollar every week.

    Here is what I have so far

    Code:
    int save = 75;
      
            for(int i = 1; i<=54; i++){
                int deposit = save+i;
                       
                
                int sum = save + deposit;
    
                System.out.println("week " + i + ". Please deposit: " + deposit + " dollars"); 
                System.out.println("You have " + sum + " dollars in the bank");
                
                 
            }

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I'm not sure if you're running total is referring to the summed saving or to the amount to deposit. Given the result of deposit, I'd suspect that's to be deposit. You'd need to move the sum before the loop and initialize it to save, then add deposit to it within the loop. Simple as that.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    Daren_Z (09-13-2013)

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    Funny, this is the sort of question a teacher would ask, not a girlfriend....
    I am the luckiest man in the world

  • #4
    New to the CF scene
    Join Date
    Sep 2013
    Location
    Edmonton
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yeah, she's trying a new way to save, so since I'm alright at math she asked me how much she would save. And I thought it would make some good practice since I haven't touched java in almost a year.


    Thanks Fou-lu, I'll try making that change to the code and see how it turns out.

  • #5
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Roelf View Post
    Funny, this is the sort of question a teacher would ask, not a girlfriend....
    Or his gf is his teacher. lol


  •  

    Tags for this Thread

    Posting Permissions

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