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 6 of 6

Thread: Cannot get out of do-while loop

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

    Default Cannot get out of do-while loop

    Hi, i'm having trouble with this assignment and the goal of this assignment is to create a 2d array that looks like this:
    0 0 0 0 0 0 0 0 0
    0 5 0 0 0 0 0 0 0
    0 0 0 0 0 7 0 0 0
    0 0 7 0 0 0 0 0 0
    0 0 0 0 7 0 0 0 4

    where the number 5 is the player and 4 is the goal and avoid the 7s which are traps. At first it got out of the do-while loop when there was only one argument inside the while() (when I tested it without hitting any 7s/traps). However, when I added more things inside the while so that the when the player hits any trap or get to the goal without hitting any trap, it would not get out of the do-while loop.

    I also want it so that the player would stay at the edge(same position) when the player accidentally move out off the board.

    This is my code:

    import java.io.*;
    import java.util.*;
     
    public class Dungeon {
     
    	static Scanner in = new Scanner (System.in);
     
    	public static void main (String[] args)
    	{
    		int [][] board= new int [5][9];
     
    		//starting point
    		int row=1;
    		int col=1;
     
    		//moves
    		int move;
    		int up= 8;
    		int right=6;
    		int left=4;
    		int down=5;
     
    		// how the map looks:
    		board[row][col]=5;
     
    		board[2][5]=7;
    		board[3][2]=7;
    		board[4][4]=7;
    		board[4][8]=4;
     
    		//print map before game
    		for (int i=0; i<board.length; i++)
    		{
    			for (int j=0; j<board[i].length; j++)
    			{
    				System.out.print(" "+ board[i][j]);
    			}
    			System.out.println("");
    		}  
     
    		do
    		{
    				System.out.println("Enter move");
    				move=in.nextInt();
     
    				if (move==up)
    				{
    					board[row][col]=0;
    					board[--row][col]=5;
    					System.out.println(row+ " "+ col);
    				}
    				else if(move==down)
    				{
    					board[row][col]=0;
    					board[++row][col]=5;
    					System.out.println(row+ " "+ col);
     
    				}
    				else if (move==right)
    				{
    					board[row][col]=0;
    					board[row][++col]=5;
    					System.out.println(row+ " "+ col);
    				}
    				else if (move==left)
    				{
    					board[row][col]=0;
    					board[row][--col]=5;
    					System.out.println(row+ " "+ col);
    				}
     
    				for (int i=0; i<board.length; i++)
    				{
    					for (int j=0; j<board[i].length; j++)
    					{
    						System.out.print(" "+ board[i][j]);
    					}
    					System.out.println("");
    				}  
     
    		} while (board[4][8]!=5 ||board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5);
     
    		if (board[4][8]==5)
    		{
     
    			System.out.println("WIN");
    		}
    		else if (board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5)
    		{
    			System.out.println("LOSE");
    		}
     
    	}
    }

    Thanks in advance!
    Last edited by launchy; April 16th, 2012 at 07:10 PM.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Cannot get out of do-while loop

    If I understand the logic of the loop only one cell ever has the value 5. So

    board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5

    this expression will always be true. That is, either 5 is not located at (2,5) or it is not located at (3,2) because it can't be at both.

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

    Default Re: Cannot get out of do-while loop

    Quote Originally Posted by pbrockway2 View Post
    If I understand the logic of the loop only one cell ever has the value 5. So

    board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5

    this expression will always be true. That is, either 5 is not located at (2,5) or it is not located at (3,2) because it can't be at both.
    Right, i realized that i shouldve made
    board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5

    to
    board[2][5]==5 ||board[3][2]==5 || board[4][4]==5

    but still doesnt work any ideas to why?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Cannot get out of do-while loop

    What does "doesnt work" mean? In particular, under what circumstances is the loop supposed to finish?

    Remember the condition you put in the while part is the condition that means the loop keeps going.

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

    Default Re: Cannot get out of do-while loop

    Quote Originally Posted by pbrockway2 View Post
    What does "doesnt work" mean? In particular, under what circumstances is the loop supposed to finish?

    Remember the condition you put in the while part is the condition that means the loop keeps going.
    Thanks for this! I rethinked the way I approached this and used a while looop! Working now! Here's the code
    Now just gotta work on making the player stay on the same location when he accidentally gets off the board
    		while (flag=true)
    		{
    				System.out.println("Enter move");
    				move=in.nextInt();
     
    				if (move==up)
    				{
    					board[row][col]=0;
    					board[--row][col]=5;
    					System.out.println(row+ " "+ col);
     
    				}
    				else if(move==down)
    				{
    					board[row][col]=0;
    					board[++row][col]=5;
    					System.out.println(row+ " "+ col);
     
    				}
    				else if (move==right)
    				{
    					board[row][col]=0;
    					board[row][++col]=5;
    					System.out.println(row+ " "+ col);
    				}
    				else if (move==left)
    				{
    					board[row][col]=0;
    					board[row][--col]=5;
    					System.out.println(row+ " "+ col);
    				}
     
    				if (board[4][8]==5)
    				{
     
    					System.out.println("WIN");
    					break;
    				}
    				else if (board[2][5]==5 ||board[3][2]==5 || board[4][4]==5)
    				{
    					System.out.println("LOSE");
    					break;
    				}
     
     
    				for (int i=0; i<board.length; i++)
    				{
    					for (int j=0; j<board[i].length; j++)
    					{
    						System.out.print(" "+ board[i][j]);
    					}
    					System.out.println("");
    				}  
     
    		}

  6. #6
    Member
    Join Date
    Jan 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Cannot get out of do-while loop

    while (flag=true)
    That just makes flag true every iteration, you mean while(flag==true) which honestly can just be while(flag)

Similar Threads

  1. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  2. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  3. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  4. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM