Loop conditions not working
I have the following code for entering loops:
public void play()
{
System.out.println(toString());
while(player.getPosition() != exit.getPosition() && itemsCollected() == false ){
turn();
System.out.println(toString());
if(difficulty=='h'){
if(e1.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
else if(e2.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
}
}
The part in bold does not work, because whenever it runs the loop will cease execution when player.getPosition() = exit.getPosition even if items collected = false, and i cant figure out why?
Re: Loop conditions not working
We have little context to provide advice...for example what does getPosition return? Boolean, String, integer? It helps to post an SSCCE which defines the problem with the minimum number of lines of code
Re: Loop conditions not working
Quote:
Originally Posted by
bmxershane
I have the following code for entering loops:
public void play()
{
System.out.println(toString());
while(player.getPosition() != exit.getPosition() && itemsCollected() == false ){
turn();
System.out.println(toString());
if(difficulty=='h'){
if(e1.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
else if(e2.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
}
}
The part in bold does not work, because whenever it runs the loop will cease execution when player.getPosition() = exit.getPosition even if items collected = false, and i cant figure out why?
If itemsCollected() returns false, itemsCollected() == false is true. Therefor, the entire while true when the first statement it true and the the second false.
Does the loop execute when the first statement is true and itemsCollected() returns true?
All this of course assume that itemsCollected() returns a boolean value.
Re: Loop conditions not working
sorry guys, itemsCollected() is boolean and player.getPosition() is an int. The problem im having is that if the players position(player.getPosition()) does equal the exit position (exit.getPosition()) but the items are not all collected (so itemsCollected returns false) the loop will not execute. The result i want to achieve works when the code is programmed as follows:
Code :
while(player.getPosition() != exit.getPosition() ){
turn();
System.out.println(toString());
if(difficulty=='h'){
if(e1.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
else if(e2.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
}
}
while(itemsCollected() == false ){
turn();
System.out.println(toString());
if(difficulty=='h'){
if(e1.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
else if(e2.getPosition()==player.getPosition()){
System.out.println("Ouch! You were eaten. ");
System.exit(0);}
}
}
Re: Loop conditions not working
oh my god guys thanks for your help but i found out what was wrong, i was meant to be using the || to say OR rather than using AND to achieve what i wanted, feel like a goose now hahaha