1onic 1nferno
1onic 1nferno

Reputation: 21

Alternating patterns in java

Hello in trying to figure out how to make the following pattern in java

 xoxox
 oxoxo
 xoxox

Im recently learning and have been trying to figure it out for hours. Here is the code that i have so far. It mainly has to do with the last part under public static String textBoxString(int rows, int cols, char c1, char c2)

public class TextBoxTester {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String s = TextBox.textBoxString(3);
        System.out.println(s);

        s = TextBox.textBoxString(4, '+');
        System.out.println(s);

        s = TextBox.textBoxString(3, 4);
        System.out.println(s);

        s = TextBox.textBoxString(3, 5, 'x', 'o');
        System.out.println(s);

    }
}

public class TextBox {

    public static String textBoxString(int side) {
        String s = "";
        for (int i = 0; i < side; i++) {
            for (int j = 0; j < side; j++)
                s += "*";
            s += "\n";
        }
        return s;
    }

    public static String textBoxString(int side, char bChar) {
        String s = "";
        for (int i = 0; i < side; i++) {
            for (char j = 39; j < bChar; j++)
                s += bChar;
            s += "\n";
        }
        return s;
    }

    public static String textBoxString(int rows, int cols) {
        String s = "";
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++)
                s += "*";
            s += "\n";
        }
        return s;
    }

    public static String textBoxString(int rows, int cols, char c1, char c2) {
        char c = c1;
        char d = c2;
        String s = "";
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++)
                s += c;
                    if (c == c1) {
                        c2++;
                    } else {
                        c1 = c;
                    }
                    s += "\n";
        }
        return s;
    }
}

Upvotes: -3

Views: 1224

Answers (2)

Jugal Kishor Saini
Jugal Kishor Saini

Reputation: 1

public static String textBoxString(int rows, int cols, char c1, char c2) {

        String s = "";

        for (int i = 0; i < rows; i++) {

            if (i == 0 || i == rows - 1) {
                for (int j = 0; j < cols; j++) {
                    // this is used for alternating columns
                    if (j % 2 == 0) { // if j is even then append c1
                        s += c1;
                    } else {
                        s += c2; // // if j is odd then append c2
                    }
                }

            } else {

                // this is used for alternating rows
                char tmp = c1;
                if (i % 2 == 0) { // switch the values of c1 and c2
                    c1 = c2;
                    c2 = tmp;
                } else { // switch the values of c1 and c2
                    tmp = c2;
                    c2 = c1;
                    c1 = tmp;
                }

                s += c1;

                for (int j = 0; j < cols - 2; j++) {

                    s += " ";
                }

                s += c2;
            }

            s += "\n";
        }

        return s;
    }

output: xoxox o x x o xoxox

Upvotes: 0

Mark Melgo
Mark Melgo

Reputation: 1478

I have updated this function below:

public static String textBoxString(int rows, int cols, char c1, char c2) {
    String s = "";
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
                        // this is used for alternating columns
            if (j % 2 == 0) { // if j is even then append c1
                s += c1;
            } else {
                s += c2; // // if j is odd then append c2
            }
        }

                // this is used for alternating rows
        char tmp = c1;
        if (i % 2 == 0) { // switch the values of c1 and c2
                c1 = c2;
            c2 = tmp;
        } else { // switch the values of c1 and c2
            tmp = c2;
            c2 = c1;
            c1 = tmp;
        }

        s += "\n";
    }
    return s;
}

Upvotes: 1

Related Questions