Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Loop conditions not working

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. #3
    Member
    Join Date
    Jun 2010
    Posts
    75
    Thanks
    7
    Thanked 1 Time in 1 Post

    Default Re: Loop conditions not working

    Quote Originally Posted by bmxershane View Post
    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.

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

     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);}
                }
     
            }

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Hangman help. Comparing arrays and conditions.
    By javanewbie1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 18th, 2011, 09:26 AM
  2. [SOLVED] Thread.sleep() in while loop not working
    By mds1256 in forum Threads
    Replies: 4
    Last Post: January 13th, 2011, 06:02 AM
  3. [SOLVED] Loop Breaking Not Working
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2010, 09:06 PM
  4. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM