Results 1 to 6 of 6
  1. #1
    Jonotron is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default [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:
    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();
          }
        }
    I was able to correct the problem by just reversing it like this:
    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();
          }
        }
    And the desired output should be similar to "Conway's Game of Life" as this is supposed to be a replica.
    Last edited by Jonotron; 11-25-2009 at 02:36 AM. Reason: Solved

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    236
    Rep Power
    11

    Default

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

  3. #3
    Jonotron is offline Member
    Join Date
    Nov 2009
    Posts
    4
    Rep Power
    0

    Default

    Oh wow, now that I think about it, that makes so much more sense. Thanks a lot!

  4. #4
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    236
    Rep Power
    11

    Default

    Glad I could help.:)

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    12

    Default

    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!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    32

Similar Threads

  1. explanation of this loop?
    By glopez09 in forum New To Java
    Replies: 4
    Last Post: 11-15-2009, 02:36 AM
  2. JAVA: String char removal with nested loop
    By igniteflow in forum New To Java
    Replies: 3
    Last Post: 11-28-2008, 02:09 AM
  3. Replies: 4
    Last Post: 09-05-2008, 04:47 PM
  4. nested for loop question
    By javabob in forum New To Java
    Replies: 3
    Last Post: 05-20-2008, 11:00 PM
  5. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •