Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: paying off dept ( can't make it stop at 0 )

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation paying off dept ( can't make it stop at 0 )

    Heey,

    I'm having a problem with this code. It doesn't want to stop at 0.

    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            double schuld = 100000;
            double rp = 9.0;
            double rente;
            double af = 15000;
            double sum;
            int jaar = 1;
     
            sum = 0;
            rente = (schuld/100) * rp;
     
            System.out.println("Jaar   Schuld   Rp   Rente  Aflossing");
            System.out.println(jaar + "    " + schuld + "    " + rp + "   " + rente + "   " + af);
     
     
            while (schuld >= 0){
     
                rente = (schuld/100) * rp;
                schuld = (schuld + rente) - af;
                jaar++;
                System.out.println(jaar + "    " + schuld + "    " + rp + "   " + rente + "   " + af);
           if(schuld == 0)
               schuld = sum;
            }
         }
    }

    Can anyone please help me?


  2. #2
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: paying off dept ( can't make it stop at 0 )

    Next time please surround your code in [highlight=java][/highlight] tags.
    I'm having a problem with this code. It doesn't want to stop at 0.
    Well... Your while loop says >= 0. Either change your loop to
    while (schuld > 0){

    or change this line
    sum = -1;

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,593
    Thanks
    24
    Thanked 312 Times in 292 Posts

    Default Re: paying off dept ( can't make it stop at 0 )

    paying off dept ( can't make it stop at 0 )
    Isn't that more like real life though?

    Seriously though, Brty93yoda is correct. The case you specify in the while loop is the case in which it will leave. This is important because the case is evaluated BEFORE it runs the loop (unlike a Do-While). So when schuld is equal to 0 and you are expecting it to do the loop 1 more time, it actually meets the condition of schuld>=0 and it leaves the loop right then.

  4. #4
    Member
    Join Date
    May 2010
    Posts
    36
    Thanks
    0
    Thanked 13 Times in 12 Posts

    Default Re: paying off dept ( can't make it stop at 0 )

    hello bossmeister, your while-loop runs only until the variable schuld is >= 0 and then stops. the schuld variable is calculated with the formula (schuld + rente) - af. in order to become 0, you must adapt your formulara arguments or the formula, otherwise the debitor will overpay his debt. i think this is not a java but a financial issue.
    Last edited by j2me64; September 16th, 2010 at 07:49 AM.

Similar Threads

  1. [SOLVED] Why is my float automatically rounding and how do i get it to stop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 18
    Last Post: August 26th, 2010, 01:41 PM
  2. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM
  3. Trying to make a bot
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 11th, 2010, 02:07 PM
  4. Replies: 4
    Last Post: April 27th, 2010, 01:18 AM
  5. radio buttons stop working
    By tabutcher in forum AWT / Java Swing
    Replies: 2
    Last Post: March 5th, 2010, 08:28 AM