Results 1 to 5 of 5
Thread: Issues with a while loop
- 11-09-2007, 05:47 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
Issues with a while loop
The code is below. What is happening is that my while statement is never evaluating to false when it should be.
I did a System.out.println(goDir) after I set it to true. It showed the value to be equal to true. Shouldn't the while loop stop as soon as the statement is false? I also tried a
And that didn't seem to work either. Does anyone see anything wrong with this code? Also, i is always greater than 1 when this while statement goes off. So lastHit[i-1] does exist.Java Code:do{ } while(!goDir && lastHit[i][6] > 0);
Java Code:while (!goDir && lastHit[i][6] > 0){ System.out.println("goDir = " + goDir); System.out.println("lastHit[i][6] = " + lastHit[i][6]); if (lastHit[i][6] > 0){ System.out.println("X Difference = " + (startX - lastHit[i-1][0])); System.out.println("2 = " + lastHit[i][2]); System.out.println("3 = " + lastHit[i][3]); System.out.println("4 = " + lastHit[i][4]); System.out.println("5 = " + lastHit[i][5]); if ((startX - lastHit[i-1][0]) > 0){ System.out.println("X + 1"); if (lastHit[i][2] == 1){ if ((startX + 1) <= boardWidth){ startX++; lastHit[i][2] = 0; lastHit[i][6]--; goDir = true; }else{ lastHit[i][2] = 0; lastHit[i][6]--; } } } else if ((startX - lastHit[i-1][0]) < 0){ System.out.println("X - 1"); if (lastHit[i][3] == 1){ if ((startX - 1) >= 0){ startX--; lastHit[i][3] = 0; lastHit[i][6]--; goDir = true; }else{ lastHit[i][3] = 0; lastHit[i][6]--; } } } if ((startY - lastHit[i-1][1]) > 0){ System.out.println("Y + 1"); if (lastHit[i][4] == 1){ if ((startY + 1) <= boardHeight){ startY++; lastHit[i][4] = 0; lastHit[i][6]--; goDir = true; }else{ lastHit[i][4] = 0; lastHit[i][6]--; } } } else if ((startY - lastHit[i-1][1]) < 0){ System.out.println("Y - 1"); if (lastHit[i][5] == 1){ if ((startY - 1) >= 0){ startY--; lastHit[i][5] = 0; lastHit[i][6]--; goDir = true; }else{ lastHit[i][5] = 0; lastHit[i][6]--; } } } } }Last edited by Lang; 11-09-2007 at 05:49 PM.
- 11-09-2007, 07:41 PM #2
Without knowing more of what is going on, the only thing that I can think of is that startX - lastHit[i-1][1] and startY - lastHit[i-1][1] might be equal to 0. This is a case for which you are not checking. I would add a catch-all else to your if else's just to make sure.
From what you have said before, it sounds like you have gotten it to print 'true' at some point. If that is the case, I'm not really sure what to tell you.
Another minor detail; your main if isn't necessary. It is reiterating one of your loop conditions. You might have put it in there for testing though. I have no idea.
- 11-09-2007, 07:47 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
I did put the main if there for testing. Also the reason I don't have an else is because either the startX or startY are going to be different, no matter what. However, I'll add it in and see what happens.
Thanks for taking a look! Ill post the result of the changes soon.
- 11-09-2007, 08:07 PM #4
Member
- Join Date
- Nov 2007
- Posts
- 8
- Rep Power
- 0
ShoeNinja, you were right. Those if statements evaluated to 0. I'll fix 'er up now.
Thanks for all your help.
- 11-09-2007, 09:01 PM #5
Similar Threads
-
iterator issues
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:43 PM -
Issues with Jva I.O
By Annatar01 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:16 AM -
Array issues
By Neo82 in forum New To JavaReplies: 1Last Post: 12-31-2007, 03:22 AM -
Compatibility issues with IE7
By Aneesha in forum New To JavaReplies: 7Last Post: 11-30-2007, 05:46 AM -
Variable issues!
By Alex89 in forum New To JavaReplies: 1Last Post: 11-07-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks