I'm trying to make a method that takes in an integer parameter with user input, checks whether it's odd or even, and then creates a corresponding pattern of x's and o's depending on which it is. The amount of rows and columns is equal to the value of the parameter as well.
The pattern that should be output if the parameter is odd is a cross. It if equals 5 it should look like this:
xooox
oxoxo
ooxoo
oxoxo
xooox
The pattern that should be output if the parameter is even is a kind of border. If the input is 6, it looks like this:
xxxxxx
xoooox
xoooox
xoooox
xoooox
xxxxxx
I've tried multiple ways of doing the odd pattern, but can't quite figure out how to do it. The closest I've got is by modifying a version of this thread: How to create an "X" pattern? My current attempts look like this:
public class squarePattern {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int userInt = scan.nextInt();
createSquare(userInt);
}
public static void createSquare(int intIn) {
for(int i =0; i < intIn; i++){
for(int j = 0; j < intIn; j++){
if(j==2*i || 8-2*i == j)
System.out.print("x");
else
System.out.print("o");
}
System.out.println();
}
}
}
I'm not sure how to adapt the diagonal line formula to the intIn
parameter, and was wondering if someone could help me figure it out. I also have not a clue how to do the pattern for when the parameter is even, so any help with that would be great as well.
The fixed border pattern for those reading:
if (intIn % 2 == 0) {
for (int row = 0; row < intIn; row++) {
for (int col = 0; col < intIn; col++) {
if (row == 0 || row == intIn - 1)
System.out.print("x");
else if (col == 0 || col == intIn - 1)
System.out.print("x");
else System.out.print("o");
}
System.out.println();
}
}
output:
Enter a positive integer: 4
xxxx
xoox
xoox
xxxx