Results 1 to 5 of 5

Thread: Checkerboard

  1. #1
    mbw290 is offline Member
    Join Date
    Sep 2011
    Posts
    12
    Rep Power
    0

    Default Checkerboard

    Im trying to make Xs and Os to look like this
    OXOXOXOX
    XXXXXXXX
    OXOXOXOX
    XXXXXXXX
    OXOXOXOX
    XXXXXXXX
    OXOXOXOX
    XXXXXXXX
    but my output is this
    XOXOXOXO
    XXXXXXXX
    XOXOXOXO
    XXXXXXXX
    XOXOXOXO
    XXXXXXXX
    XOXOXOXO
    XXXXXXXX

    my code is this
    Java Code:
    public class Checkerboard 
    { 
      public static void main(String[] args) 
      { 
        for(int i=0;i<8;i++){ 
          for(int j=0;j<8;j++){ 
            if((j)%2==0)
             System.out.print("X");
            else if ((i)%2==0)
            System.out.print("O");
            else{System.out.print("X");}
          }
             System.out.println(""); 
          } 
          }
      }
    any ideas?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default Re: Checkerboard

    Looks like you need to print a 0 at the end of some lines in stead of at the beginning.
    Is that the problem?

    Add some printlns to your code to show the values of the variables that controls whether a 0 or an X is being printed.
    Change the logic so the lines start with the letter you want.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Checkerboard

    You probably want nested if statements rather than a 3 branch if statement.

  4. #4
    dinesh.guntha is offline Member
    Join Date
    May 2011
    Posts
    27
    Rep Power
    0

    Smile Re: Checkerboard

    public class Checkerboard
    {
    public static void main(String[] args)
    {
    for(int i=0;i<8;i++){
    for(int j=0;j<8;j++){

    if(i%2 == 0) {
    if(j%2 == 0)
    System.out.print("O");
    else
    System.out.print("X");
    } else {
    System.out.print("X");
    }
    }
    System.out.println("");
    }
    }
    }

    output :

    OXOXOXOX
    XXXXXXXX
    OXOXOXOX
    XXXXXXXX
    OXOXOXOX
    XXXXXXXX
    OXOXOXOX
    XXXXXXXX

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Checkerboard

    Do you want a pat on the head Dinesh? In future do not post your solution. The assignment is for the OP to write it, not you.

Similar Threads

  1. checkerboard help
    By armedrabbit in forum New To Java
    Replies: 1
    Last Post: 04-03-2011, 08:23 AM
  2. Simple Checkerboard
    By ShortIt in forum New To Java
    Replies: 2
    Last Post: 01-19-2011, 05:03 AM
  3. Checkerboard Program
    By Jnoobs in forum New To Java
    Replies: 9
    Last Post: 09-21-2010, 04:46 AM
  4. Replies: 3
    Last Post: 07-23-2009, 08:45 AM
  5. Replies: 1
    Last Post: 07-22-2009, 11:10 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
  •