简体   繁体   中英

Java Do While loop skipping condition

I have a do while loop that is terminating without the condition being met and I'm not sure why. When I debug check's row and columns are (4,10) and goal's are (4,1). Any ideas?

Position check = unexplored.get();
do{

    if(isValid(check.up())){                       
        unexplored.put(check.up());
        numUnexplored++;
    }
    if(isValid(check.right())){
        unexplored.put(check.right());
        numUnexplored++;
    }
    if(isValid(check.down())){
        unexplored.put(check.down());
        numUnexplored++;
        }
    if(isValid(check.left())){
        unexplored.put(check.left());
        numUnexplored++;
        }
    explore(check);
    explored.put(check);
    check = unexplored.get(); 

 }while(check.getColumn() != goal.getColumn() && check.getRow() != goal.getRow());

It's worth noting that if I change checks initialization at the top to:

Position check = null;

and then set it at the top of the loop, like:

do{
    check = unexplored.get();
    ....

the loop terminates at the same spot.

If you want to exit only when check and goal refer to the same cell, then you'll need to change && to || in your while condition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM