java中的交替模式

java中的交替模式,java,alternating,Java,Alternating,您好,我正在尝试找出如何在java中创建以下模式 xoxox oxoxo xoxox 我最近在学习,几个小时来一直在想办法。这是我目前掌握的代码。这主要与下面的最后一部分有关 公共静态字符串textBoxString(int行、int列、字符c1、字符c2) 公共类TextBoxTester{ 公共静态void main(字符串[]args){ //TODO自动生成的方法存根 字符串s=TextBox.textBoxString(3); 系统输出打印项次; s=TextBox.textB

您好,我正在尝试找出如何在java中创建以下模式

 xoxox
 oxoxo
 xoxox
我最近在学习,几个小时来一直在想办法。这是我目前掌握的代码。这主要与下面的最后一部分有关 公共静态字符串textBoxString(int行、int列、字符c1、字符c2)

公共类TextBoxTester{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
字符串s=TextBox.textBoxString(3);
系统输出打印项次;
s=TextBox.textBoxString(4,“+”);
系统输出打印项次;
s=TextBox.textBoxString(3,4);
系统输出打印项次;
s=TextBox.textBoxString(3,5,'x','o');
系统输出打印项次;
}
}
公共类文本框{
公共静态字符串textBoxString(内部端){
字符串s=“”;
对于(int i=0;i
我已将此功能更新如下:

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;
}
publicstaticstringtextboxstring(int行、int列、charc1、charc2){
字符串s=“”;
对于(int i=0;i
公共静态字符串textBoxString(int行、int列、字符c1、字符c2){
字符串s=“”;
对于(int i=0;i
输出: xoxox 氧 xO
xoxox

将其标记为已接受的答案,以便解决此问题
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;
}
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;
    }