Results 1 to 11 of 11
Thread: trouble with loop
 10152009, 04:01 AM #1Member
 Join Date
 Oct 2009
 Posts
 8
 Rep Power
 0
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; }
Java Code:int j =0; while(j<totalToothpicks ){ for(i=1; i<pickUpAtTime; i++) j+=i*(pickUpAtTime+1)+1; }
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; 10152009 at 09:30 PM.
 10152009, 04:23 AM #2
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,370
 Blog Entries
 1
 Rep Power
 21
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?
 10152009, 08:52 AM #3Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 9
Why are you saying
Java Code:j=i*(3+1)+1
Java Code:j=i*4 + 1
 10152009, 09:32 PM #4Member
 Join Date
 Oct 2009
 Posts
 8
 Rep Power
 0
r035198x, sorry, I fixed the code, but the loop still doesn't work!!
 10152009, 10:20 PM #5Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
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; }
Java Code:pickUpAtTime = 3; int j =0; while(j<totalToothpicks ){ for(i=1; i<3; i++) j+=i*(3+1)+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.
 10162009, 07:08 PM #6Member
 Join Date
 Oct 2009
 Posts
 8
 Rep Power
 0
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!
 10162009, 07:26 PM #7Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
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
 10162009, 07:27 PM #8Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
If you want it to check the j value after every iteration don't use a for loop inside of a while.
 10162009, 07:31 PM #9Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 9
We can't see what your current code looks like now so we can't say too much.
 10162009, 07:44 PM #10Member
 Join Date
 Oct 2009
 Posts
 8
 Rep Power
 0
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?
\
 10162009, 09:18 PM #11Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
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

whileloop stopping on first loop
By davester in forum New To JavaReplies: 6Last Post: 06262009, 08:46 PM 
Here comes trouble... :)
By sargehendricks in forum IntroductionsReplies: 1Last Post: 04232009, 03:18 PM 
[SOLVED] Trouble with this loop!
By PureAwesomeness in forum New To JavaReplies: 35Last Post: 02022009, 08:04 PM 
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07212008, 11:52 PM 
Trouble with For loop and variables in a program
By dablyz in forum New To JavaReplies: 12Last Post: 05062008, 04:25 AM
Bookmarks