Results 1 to 9 of 9
  1. #1
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default Why doesn't my loop work?

    I am creating a program where it simulates a ball with default direction and position. By when it strikes the corners N and M, it changes directions accordingly. The problem i am facing right now is that it only processes 1 step in my loop.

    I am not sure why my loop does not work. Can anyone comment/advice what i have done wrong in my codings?
    These are the codes below. Thanks!


    /**
    * Create a simulation that makes the ball move within the board size
    * 1--Create the driver to test the retrace path method
    */
    import java.util.*;

    public class prac3 {

    public static void main(String[] args) {
    //variables
    Scanner input = new Scanner(System.in);

    int startX, startY, N, M;

    System.out.println("Please enter board dimensions N M");
    String inWidth = input.next();
    String inHeight = input.next();
    System.out.println("Please enter the starting position X Y");
    String inX = input.next();
    String inY = input.next();

    startX = Integer.parseInt(inX);
    startY = Integer.parseInt(inY);
    N = Integer.parseInt(inWidth);
    M = Integer.parseInt(inHeight);

    updatePos(startX, startY, N, M);
    }

    public static int updatePos(int startX, int startY, int N, int M) {
    int xOrg = startX;
    int yOrg = startY;
    int xCur = startX;
    int yCur = startY;
    // default direction -- NE
    int xCurDir = 1;
    int yCurDir = 1;
    int step = 0;

    // update position
    do {
    // move default direction -- NE
    // move position x & y
    xCur = xCur + xCurDir;
    yCur = yCur + yCurDir;
    step++;
    System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);

    // direction at (N-1,M-1)-- SW
    if(xCur == (N-1)) {
    if(yCur == (M-1)) {
    xCurDir = -1;
    yCurDir = -1;
    }
    // direction at (N-1,yCur)-- SE
    else {
    xCurDir = 1;
    yCurDir = -1;
    }
    }
    // direction at (0,0) -- NE
    else if(xCur == 0) {
    if(yCur == 0) {
    xCur = 1;
    yCur = 1;
    }
    // direction at(0,yCur) -- SE
    else {
    xCur = 1;
    yCur = -1;
    }
    }
    }
    /** condition -- if xCur & yCur equals original x & y pos,
    * & xCurDir & yCurDir equals original direction,
    * stop loop
    ************************************************** *****/
    while(((xCur != xOrg) && (yCur == yOrg)) && ((xCurDir == 1) && (yCurDir == 1)));
    return step;
    }
    }

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    What's the purpose of having a semi-colon there?

    What's the original direction's value?
    Last edited by sukatoa; 05-26-2008 at 03:22 AM.
    freedom exists in the world of ideas

  3. #3
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    I am sorry, but which semi-colon are your referring to? Anyways i made several changes by making the object bounce once it hits the side walls. What mistake did i make to cause an endless loop? I am pretty sure my conditions are fine, but i would like to check them.


    These are my new codes:
    /**
    * Create a simulation that makes the ball move within the board size
    * 1--Create the driver to test the retrace path method
    */
    import java.util.*;

    public class prac3 {

    public static void main(String[] args) {
    //variables
    Scanner input = new Scanner(System.in);

    int startX, startY, N, M;

    System.out.println("Please enter board dimensions N M");
    String inWidth = input.next();
    String inHeight = input.next();
    System.out.println("Please enter the starting position X Y");
    String inX = input.next();
    String inY = input.next();

    startX = Integer.parseInt(inX);
    startY = Integer.parseInt(inY);
    N = Integer.parseInt(inWidth);
    M = Integer.parseInt(inHeight);

    updatePos(startX, startY, N, M);
    }

    public static int updatePos(int startX, int startY, int N, int M) {
    // original x,y coordinates
    int xOrg = startX;
    int yOrg = startY;
    // current x,y coordinates
    int xCur = startX;
    int yCur = startY;
    // previous x,y coordinates
    int xPrev = 0;
    int yPrev = 0;
    // default direction movement -- NE
    int xCurDir = 1;
    int yCurDir = 1;
    int step = 0;

    // update position
    do {
    // move default direction -- NE
    // move default position x & y
    xCur = xCur + xCurDir;
    yCur = yCur + yCurDir;
    step++;
    System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
    xPrev = xCur;
    yPrev = yCur;

    //conditions
    // xCur == (N-1)
    if(xCur == (N-1)) {
    // corner pos @ (N-1,M-1) -- direction SW
    if(yCur == (M-1)) {
    xCurDir = -1;
    yCurDir = -1;
    }
    // corner pos @ (N-1,0) -- direction NW
    else if(yCur == 0) {
    xCurDir = -1;
    yCurDir = 1;
    }
    // side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur < yPrev)) -- SW
    else if((xCur > xPrev)&&(yCur < yPrev)) {
    xCurDir = -1;
    yCurDir = -1;
    }
    // side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur > yPrev)) -- NW
    else if((xCur > xPrev)&&(yCur > yPrev)) {
    xCurDir = -1;
    yCurDir = 1;
    }
    }
    // xCur == 0
    else if(xCur == 0) {
    // corner pos @ (0,0) -- direction NE
    if(yCur == 0) {
    xCur = 1;
    yCur = 1;
    }
    // corner pos @ (0,M-1) -- direction SE
    else if(yCur == (M-1)) {
    xCurDir = 1;
    yCurDir = -1;
    }
    // side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur > yPrev)) -- NE
    else if((xCur < xPrev)&&(yCur > yPrev)) {
    xCurDir = 1;
    yCurDir = 1;
    }
    // side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur <yPrev)) -- SE
    else if((xCur < xPrev)&&(yCur < yPrev)) {
    xCurDir = 1;
    yCurDir = -1;
    }
    }
    // yCur == 0
    // side pos @ (xCur,0)&&((xCur < xPrev)&&(yCur>yPrev)) -- NW
    else if(yCur == 0) {
    if((xCur < xPrev)&&(yCur > yPrev)) {
    xCurDir = -1;
    yCurDir = 1;
    }
    // side pos @ (xCur,0)&&((xCur > xPrev)&&(yCur < yPrev)) -- NE
    else if((xCur > xPrev)&&(yCur < yPrev)) {
    xCurDir = 1;
    yCurDir = 1;
    }
    }
    // yCur == (M-1)
    else if(yCur == (M-1)) {
    // side pos @ (xCur,M-1)&&((xCur < xPrev)&&(yCur > yPrev)) -- SW
    if((xCur < xPrev)&&(yCur > yPrev)) {
    xCurDir = -1;
    yCurDir = -1;
    }
    // side pos @ (xCur,M-1)&&((xCur > xPrev)&&(yCur >yPrev)) -- SE
    else if((xCur > xPrev)&&(yCur >yPrev)) {
    xCurDir = 1;
    yCurDir = -1;
    }
    }

    xCur = xCur + xCurDir;
    yCur = yCur + yCurDir;
    step++;
    System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
    xPrev = xCur;
    yPrev = yCur;

    }
    /** condition -- if xCur & yCur equals original x & y pos,
    * & xCurDir & yCurDir equals original direction,
    * stop loop
    ************************************************** *****/
    while((xCur < N && xCur >= 0) && (yCur < M && yCur >= 0) &&
    ((xCur == xOrg) || (yCur == yOrg)) || ((xCurDir == 1) || (yCurDir == 1)));
    return step;
    }
    }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Please use code tags posting codes next time pal. :) In this it's not easy to go through the code.

  5. #5
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Sorry, am new to this. I will re-post it in quote tags as you suggested

    Java Code:
    /**
     *	Create a simulation that makes the ball move within the board size
     *	1--Create the driver to test the retrace path method
     */
     import java.util.*;
    
    public class prac3 {
    
    	public static void main(String[] args) {
    		//variables
    		Scanner input = new Scanner(System.in);
    		
    		int startX, startY, N, M;
    		
    		System.out.println("Please enter board dimensions N M");
    		String inWidth  = input.next();
    		String inHeight = input.next();
    		System.out.println("Please enter the starting position X Y");
    		String inX = input.next();
    		String inY = input.next();
    		
    		startX 	= Integer.parseInt(inX);
    		startY 	= Integer.parseInt(inY);
    		N	= Integer.parseInt(inWidth);
    		M 	= Integer.parseInt(inHeight);
    		
    		updatePos(startX, startY, N, M);
    	}
    	
    	public static int updatePos(int startX, int startY, int N, int M) {
    		//	original x,y coordinates
    		int xOrg = startX;
    		int yOrg = startY;
    		//	current x,y coordinates
    		int xCur = startX;
    		int yCur = startY;
    		//	previous x,y coordinates
    		int xPrev = 0;
    		int yPrev = 0;
    		//	default direction movement -- NE
    		int xCurDir = 1;
    		int yCurDir = 1;
    		int step = 0;
    
    		//	update position
    		do {
    			//	move default direction -- NE
    			//	move default position x & y
    			xCur = xCur + xCurDir;
    			yCur = yCur + yCurDir;
    			step++;
    			System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
    			xPrev = xCur;
    			yPrev = yCur;
    			
    			//conditions
    			//	xCur == (N-1)
    			if(xCur == (N-1)) {
    				//	corner pos @ (N-1,M-1) -- direction SW
    				if(yCur == (M-1)) {
    					xCurDir = -1;
    					yCurDir = -1;
    				}
    				//	corner pos @ (N-1,0) -- direction NW
    				else if(yCur == 0) {
    					xCurDir = -1;
    					yCurDir = 1;
    				}
    				//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur < yPrev)) -- SW
    				else if((xCur > xPrev)&&(yCur < yPrev)) {
    					xCurDir = -1;
    					yCurDir = -1;
    				}
    				//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur > yPrev)) -- NW
    				else if((xCur > xPrev)&&(yCur > yPrev)) {
    					xCurDir = -1;
    					yCurDir = 1;
    				}
    			}
    			//	xCur == 0
    			else if(xCur == 0) {
    				//	corner pos @ (0,0) -- direction NE
    				if(yCur == 0) {
    					xCur = 1;
    					yCur = 1;
    				}
    				//	corner pos @ (0,M-1) -- direction SE
    				else if(yCur == (M-1)) {
    					xCurDir = 1;
    					yCurDir = -1;
    				}
    				//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur > yPrev)) -- NE
    				else if((xCur < xPrev)&&(yCur > yPrev)) {
    					xCurDir = 1;
    					yCurDir = 1;
    				}
    				//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur <yPrev)) -- SE
    				else if((xCur < xPrev)&&(yCur < yPrev)) {
    					xCurDir = 1;
    					yCurDir = -1;
    				}
    			}
    			//	yCur == 0
    			//	side pos @ (xCur,0)&&((xCur < xPrev)&&(yCur>yPrev)) -- NW
    			else if(yCur == 0) {
    				if((xCur < xPrev)&&(yCur > yPrev)) {
    					xCurDir = -1;
    					yCurDir = 1;
    				}
    				//	side pos @ (xCur,0)&&((xCur > xPrev)&&(yCur < yPrev)) -- NE
    				else if((xCur > xPrev)&&(yCur < yPrev)) {
    					xCurDir = 1;
    					yCurDir = 1;
    				}
    			}
    			//	yCur == (M-1)
    			else if(yCur == (M-1)) {
    				//	side pos @ (xCur,M-1)&&((xCur < xPrev)&&(yCur > yPrev)) -- SW
    				if((xCur < xPrev)&&(yCur > yPrev)) {
    					xCurDir = -1;
    					yCurDir = -1;
    				}
    				//	side pos @ (xCur,M-1)&&((xCur > xPrev)&&(yCur >yPrev)) -- SE
    				else if((xCur > xPrev)&&(yCur >yPrev)) {
    					xCurDir = 1;
    					yCurDir = -1;
    				}
    			}
    			
    			xCur = xCur + xCurDir;
    			yCur = yCur + yCurDir;
    			step++;
    			System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
    			xPrev = xCur;
    			yPrev = yCur;
    
    		}
    		/**	condition -- if xCur & yCur equals original x & y pos, 
    		 *	& xCurDir & yCurDir equals original direction,
    		 *	stop loop
    		 *******************************************************/
    		while((xCur < N && xCur >= 0) && (yCur < M && yCur >= 0) &&
    			((xCur == xOrg) || (yCur == yOrg)) || ((xCurDir == 1) || (yCurDir == 1)));
    		return step;
    	}
    }

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Thanks buddy, :)

    You get the endless loop, that your while loop to fall into false. Better to check that condition. I'm not clear exactly what you want to do in that case.

  7. #7
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    You get the endless loop, that your while loop to fall into false. Better to check that condition. I'm not clear exactly what you want to do in that case.
    Thanks for the help:)
    I want the loop to work in such that:
    - the red text codes are conditions that only allow the program to process within the given values
    - the blue text codes are conditions that only when the values are met, then the program exits the loop

    Java Code:
    /**
     *	Create a simulation that makes the ball move within the board size
     *	1--Create the driver to test the retrace path method
     */
     import java.util.*;
    
    public class prac3 {
    
    	public static void main(String[] args) {
    		//variables
    		Scanner input = new Scanner(System.in);
    		
    		int startX, startY, N, M;
    		
    		System.out.println("Please enter board dimensions N M");
    		String inWidth  = input.next();
    		String inHeight = input.next();
    		System.out.println("Please enter the starting position X Y");
    		String inX = input.next();
    		String inY = input.next();
    		
    		startX 	= Integer.parseInt(inX);
    		startY 	= Integer.parseInt(inY);
    		N	= Integer.parseInt(inWidth);
    		M 	= Integer.parseInt(inHeight);
    		
    		updatePos(startX, startY, N, M);
    	}
    	
    	public static int updatePos(int startX, int startY, int N, int M) {
    		//	original x,y coordinates
    		int xOrg = startX;
    		int yOrg = startY;
    		//	current x,y coordinates
    		int xCur = startX;
    		int yCur = startY;
    		//	previous x,y coordinates
    		int xPrev = 0;
    		int yPrev = 0;
    		//	default direction movement -- NE
    		int xCurDir = 1;
    		int yCurDir = 1;
    		int step = 0;
    
    		//	update position
    		do {
    			//	move default direction -- NE
    			//	move default position x & y
    			xCur = xCur + xCurDir;
    			yCur = yCur + yCurDir;
    			step++;
    			System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
    			xPrev = xCur;
    			yPrev = yCur;
    			
    			//conditions
    			//	xCur == (N-1)
    			if(xCur == (N-1)) {
    				//	corner pos @ (N-1,M-1) -- direction SW
    				if(yCur == (M-1)) {
    					xCurDir = -1;
    					yCurDir = -1;
    				}
    				//	corner pos @ (N-1,0) -- direction NW
    				else if(yCur == 0) {
    					xCurDir = -1;
    					yCurDir = 1;
    				}
    				//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur < yPrev)) -- SW
    				else if((xCur > xPrev)&&(yCur < yPrev)) {
    					xCurDir = -1;
    					yCurDir = -1;
    				}
    				//	side pos @ (N-1,yCur)&&((xCur > xPrev)&&(yCur > yPrev)) -- NW
    				else if((xCur > xPrev)&&(yCur > yPrev)) {
    					xCurDir = -1;
    					yCurDir = 1;
    				}
    			}
    			//	xCur == 0
    			else if(xCur == 0) {
    				//	corner pos @ (0,0) -- direction NE
    				if(yCur == 0) {
    					xCur = 1;
    					yCur = 1;
    				}
    				//	corner pos @ (0,M-1) -- direction SE
    				else if(yCur == (M-1)) {
    					xCurDir = 1;
    					yCurDir = -1;
    				}
    				//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur > yPrev)) -- NE
    				else if((xCur < xPrev)&&(yCur > yPrev)) {
    					xCurDir = 1;
    					yCurDir = 1;
    				}
    				//	side pos @ (0,yCur)&&((xCur < xPrev)&&(yCur <yPrev)) -- SE
    				else if((xCur < xPrev)&&(yCur < yPrev)) {
    					xCurDir = 1;
    					yCurDir = -1;
    				}
    			}
    			//	yCur == 0
    			//	side pos @ (xCur,0)&&((xCur < xPrev)&&(yCur>yPrev)) -- NW
    			else if(yCur == 0) {
    				if((xCur < xPrev)&&(yCur > yPrev)) {
    					xCurDir = -1;
    					yCurDir = 1;
    				}
    				//	side pos @ (xCur,0)&&((xCur > xPrev)&&(yCur < yPrev)) -- NE
    				else if((xCur > xPrev)&&(yCur < yPrev)) {
    					xCurDir = 1;
    					yCurDir = 1;
    				}
    			}
    			//	yCur == (M-1)
    			else if(yCur == (M-1)) {
    				//	side pos @ (xCur,M-1)&&((xCur < xPrev)&&(yCur > yPrev)) -- SW
    				if((xCur < xPrev)&&(yCur > yPrev)) {
    					xCurDir = -1;
    					yCurDir = -1;
    				}
    				//	side pos @ (xCur,M-1)&&((xCur > xPrev)&&(yCur >yPrev)) -- SE
    				else if((xCur > xPrev)&&(yCur >yPrev)) {
    					xCurDir = 1;
    					yCurDir = -1;
    				}
    			}
    			
    			xCur = xCur + xCurDir;
    			yCur = yCur + yCurDir;
    			step++;
    			System.out.println("Step: " + step + " X: " + xCur + " Y: " + yCur);
    			xPrev = xCur;
    			yPrev = yCur;
    
    		}
    		/**	condition -- if xCur & yCur equals original x & y pos, 
    		 *	& xCurDir & yCurDir equals original direction,
    		 *	stop loop
    		 *******************************************************/
    		while([COLOR="Red"](xCur < N && xCur >= 0) || (yCur < M && yCur >= 0)[/COLOR] ||
    			([COLOR="Blue"](xCur == xOrg) || (yCur == yOrg)) || ((xCurDir == 1) || (yCurDir == 1))[/COLOR]);
    		return step;
    	}
    }

  8. #8
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    I am sorry, but which semi-colon are your referring to?
    this,
    Java Code:
    while(((xCur != xOrg) && (yCur == yOrg)) && ((xCurDir == 1) && (yCurDir == 1)));
    freedom exists in the world of ideas

  9. #9
    d0nmin0 is offline Member
    Join Date
    May 2008
    Posts
    12
    Rep Power
    0

    Default

    Java Code:
    while(((xCur != xOrg) && (yCur == yOrg)) && ((xCurDir == 1) && (yCurDir == 1)));
    It is a do-while loop:-
    If i took the ';' away, there would be a compilation error. I tried adding curly brackets. Not working too.

Similar Threads

  1. Will this applet ever work?
    By willemjav in forum Java Applets
    Replies: 4
    Last Post: 04-20-2008, 06:40 PM
  2. Pass by ref. A work around?
    By diRisig in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 08:25 PM
  3. how would i get this to work...?
    By deeadeed in forum New To Java
    Replies: 6
    Last Post: 12-06-2007, 03:58 AM
  4. what do I have to install to work with JSP
    By boy22 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-05-2007, 05:08 AM
  5. My own ClassLoader didn't work.
    By snooze-g in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2007, 12:12 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
  •