0

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
cooljcat
  • 31
  • 4
  • 1
    The square pattern is relatively easy. A loop inside a loop like you already have, for each row and column. If the row is first or last, print an X. If the column is first or last, print an X. If neither of those is true, print an O. – Michael May 21 '21 at 17:54
  • 1
    The cross pattern is slightly more tricky. First you can observe that (0,0), (1,1), (2,2) are all Xs. That conditional on its own will give you diagonal line going down and right. That's halfway there. Then there is another condition that will give you the other line line. If it doesn't match either then its a 0. You don't need any multiplication, nor any magic numbers like 8 and 2. – Michael May 21 '21 at 18:12
  • Your code for the border is almost right. You used the construct `if (...) { ... }; if (...) {...}; else { ... }` The else is only linked to the 2nd condition. So the first condition AND the else might both fire. That'll screw the whole thing up. You need to find a way to "link" all 3 conditions together, so that the else only triggers if the 1st AND 2nd are false. Don't want to spoil it for you, but you're just a few characters away ;) – Michael May 21 '21 at 18:34
  • 1
    Ah, you're totally right! Forgot to use an `else if` instead of just an `if`. Thanks for helping me figure it out :) – cooljcat May 21 '21 at 18:38

1 Answers1

2

The condition in your if for the method to print the cross pattern is easy:

  • The diagonal that goes from top-left to bottom-right is trivial, because the condition for the 'x' to print is simply j == i.
  • The other diagonal in the first row must print the 'x' when j == 4 or j == intIn - 1, the second row when j == 3 or j == intIn - 2, etc. Therefore, this condition also depends on the current row i but in a descending way. You can deduce from this that the condition for the second diagonal is j == intIn - i - 1.

The final code is:

import java.util.Scanner;

public class Main {
    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 == i || j == intIn - i - 1)
                    System.out.print("x");
                else
                    System.out.print("o");
            }
            System.out.println();
        }
    }
}
JMSilla
  • 1,326
  • 10
  • 17