-
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
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?
-
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.
-
Re: Checkerboard
You probably want nested if statements rather than a 3 branch if statement.
-
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
-
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.