Results 1 to 5 of 5
  1. #1
    Lang is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default 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

    Java Code:
    do{
    
    } while(!goDir && lastHit[i][6] > 0);
    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:
    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.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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.

  3. #3
    Lang is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    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.

  4. #4
    Lang is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    ShoeNinja, you were right. Those if statements evaluated to 0. I'll fix 'er up now.

    Thanks for all your help.

  5. #5
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

Similar Threads

  1. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 01:43 PM
  2. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM
  3. Array issues
    By Neo82 in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 03:22 AM
  4. Compatibility issues with IE7
    By Aneesha in forum New To Java
    Replies: 7
    Last Post: 11-30-2007, 05:46 AM
  5. Variable issues!
    By Alex89 in forum New To Java
    Replies: 1
    Last Post: 11-07-2007, 07:51 PM

Posting Permissions

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