Results 1 to 6 of 6
- 11-24-2009, 09:49 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
[SOLVED] Explanation of Nested Loop (very strange)
Hey, I've been programming for a long time, but I'm new to java (I know it's still very similar to others). I'm having a strange result from a board(like chess) I'm making:
Here is the variable init.
Java Code://Board Dimensions public static final int WIDTH = 20; public static final int HEIGHT = 10; //I'm using boolean because it's either a dot or an asterisk... public static boolean[][] board = new boolean[WIDTH][HEIGHT];
When I drew the board, the x and y flipped positions for some reason.
Originally I had this code:
I was able to correct the problem by just reversing it like this:Java Code:static void drawBoard() { for (int x = 0; x < WIDTH; x++) { for (int y = 0; y < HEIGHT; y++) { if (board[x][y] == true) { System.out.print( '*' ); } else { System.out.print( '.' ); } } System.out.println(); } }
And the desired output should be similar to "Conway's Game of Life" as this is supposed to be a replica.Java Code:static void drawBoard() { //HAVE TO START WITH COLUMN AND INVERSE THE X Y for (int x = 0; x < HEIGHT; x++) { for (int y = 0; y < WIDTH; y++) { if (board[y][x] == true) //NEED TO INVERT X AND Y TO MAKE WORK { System.out.print( '*' ); } else { System.out.print( '.' ); } } System.out.println(); } }Last edited by Jonotron; 11-25-2009 at 02:36 AM. Reason: Solved
- 11-24-2009, 10:20 PM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Hello, in your first for loop, you print the heights and then make a new line, and then print the next column, I think what you want to do is print the entire width then make a newline, and then print the next widths. Like this:
Java Code:static void drawBoard() { for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { if (board[x][y] == true) { System.out.print( '*' ); } else { System.out.print( '.' ); } } System.out.println(); } }
- 11-25-2009, 02:35 AM #3
Member
- Join Date
- Nov 2009
- Posts
- 4
- Rep Power
- 0
Oh wow, now that I think about it, that makes so much more sense. Thanks a lot!
- 11-25-2009, 03:20 AM #4
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
Glad I could help.:)
- 01-08-2011, 06:15 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
I'm really starting to get annoyed by these ad posts... reported.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
Similar Threads
-
explanation of this loop?
By glopez09 in forum New To JavaReplies: 4Last Post: 11-15-2009, 02:36 AM -
JAVA: String char removal with nested loop
By igniteflow in forum New To JavaReplies: 3Last Post: 11-28-2008, 02:09 AM -
I want to recreate the method drawRect with a nested for-loop
By chelseacortez in forum Java 2DReplies: 4Last Post: 09-05-2008, 04:47 PM -
nested for loop question
By javabob in forum New To JavaReplies: 3Last Post: 05-20-2008, 11:00 PM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks