Results 1 to 11 of 11
  1. #1
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default trouble with loop

    I'm having trouble with a loop. Basically I want to calculate a variable "i" such that it is less than or equal to a number typed in by the user, which i'm calling pickUpAtTime. As well, this variable i must satisfy the equation j=i*(3+1)+1 such that j is less than or equal to a number typed in by the user, which i'm calling totalToothpicks.

    Oh yeah, and i'm trying to calculate the max i that will satisfy j<totalToothpicks, which is why i did the for loop.

    So right now i've got:

    EDIT:
    Java Code:
    int j =0;
    while(j<totalToothpicks ){
    		for(i=1; i<pickUpAtTime; i++)
    		
    		
    			j+=i*(3+1)+1;
    		
    		
    		}
    is supposed to be:
    Java Code:
    int j =0;
    while(j<totalToothpicks ){
    		for(i=1; i<pickUpAtTime; i++)
    		
    		
    			j+=i*(pickUpAtTime+1)+1;
    		
    		
    		}
    but anyway that doesn't matter.

    Why doesn't the for loop run, testing each instance of i=1 to i<pickUpAtTime, making sure that when each instance of i is plugged into the formula for j that j is less than totalToothpicks? Because for example, I'm getting answers like 13 for j when totalToothpicks is 12??
    Last edited by aamster; 10-15-2009 at 10:30 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What I can see in your code is that simply added number from 1 to the value which is user entered. Can you send the complete error you get?

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why are you saying
    Java Code:
    j=i*(3+1)+1
    not
    Java Code:
    j=i*4 + 1
    ?

  4. #4
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    r035198x, sorry, I fixed the code, but the loop still doesn't work!!

  5. #5
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    First of all you havent given totalToothpicks a value. But if you did and forgot to post it, it's because J is inside your for loop and is more than incrementing before it exits the for loop.

    For example if a user enters 3, substitute all the pickUpAtTime with 3

    Java Code:
    pickUpAtTime = 3;
    
    int j =0;
    while(j<totalToothpicks ){
    		for(i=1; i<pickUpAtTime; i++)
    		
    		
    			j+=i*(pickUpAtTime+1)+1;
    			
    		}
    would equal
    Java Code:
    pickUpAtTime = 3;
    
    int j =0;
    while(j<totalToothpicks ){
    		for(i=1; i<3; i++)
    		
    		
    			j+=i*(3+1)+1;
    		
    		
    		}
    with this i=1
    and j=0
    j = (0) + (1)*(3+1)+1 which is the same as j = 5;

    now go through the loop again since i=1 which is still less than 3.
    This time i=2 and j=5
    j = (5) + (2)*(3+1) + 1 which is the same as j = 14;

    now i = 3 which is not less than pickUpAtTime so it will exit the for loop.

    Now it goes back to the while loop and checks if j < totalToothpicks.

  6. #6
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    ok, so i don't know what's going on. I had the program print out the i values and j values inside the loop and it returned:
    "i=0
    j=1
    i=1
    j=5
    i=2
    j=9
    i=3
    j=13
    There are 11 left.
    The computer takes 4 toothpicks."

    where the 4 in the "4 toothpicks" at the end is the i value. It only printed up to 3, though? So why is it printing 4 in this statement?? Also, for the above example, i had the totalToothpicks value as 12. But in the while loop i thought that it would check the value it returned from the for loop "13" and make sure that it is less than the totalToothpicks value. 13 is not less than 12! So, how do i write the loop so that it checks the j value after each iteration to make sure that it is less than the totalToothpicks value?

    Sorry for the many questions, but hope you can help!

  7. #7
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
    pickUpAtTime = 3;
    
    int j =0;
    while(j<totalToothpicks ){
    		for(i=1; i<3; i++)
    		
    		
    			j+=i*(3+1)+1;
    		
    		
    		}
    ok, so i don't know what's going on. I had the program print out the i values and j values inside the loop and it returned:
    "i=0
    j=1
    i=1
    j=5
    i=2
    j=9
    i=3
    j=13
    There is no possible way it can print i = to 0 if you are setting i = 1 in your for loop

  8. #8
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    If you want it to check the j value after every iteration don't use a for loop inside of a while.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    We can't see what your current code looks like now so we can't say too much.

  10. #10
    aamster is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    sorry toymachine, change i=1 to i=0 in the for loop, and also what would you suggest to help fix the problem?
    How can I check the j value after every iteration?
    \

  11. #11
    toymachiner62 is offline Member
    Join Date
    Oct 2009
    Posts
    63
    Rep Power
    0

    Default

    Post your updated code for us to help you. the problem is you are going through a loop inside of a loop.

    Study this diagram, it should help with nested loops
    http://users.evtek.fi/~jaanah/IntroC...flow_nest1.gif

Similar Threads

  1. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  2. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 04:18 PM
  3. [SOLVED] Trouble with this loop!
    By PureAwesomeness in forum New To Java
    Replies: 35
    Last Post: 02-02-2009, 08:04 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-22-2008, 12:52 AM
  5. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 05:25 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
  •