Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法在偶数列上打印主字符,在奇数列上打印次字符_Java - Fatal编程技术网

Java 方法在偶数列上打印主字符,在奇数列上打印次字符

Java 方法在偶数列上打印主字符,在奇数列上打印次字符,java,Java,我必须创建一个方法,它将在偶数列上打印主字符,在奇数列上打印次字符 下面是所需的输出 . - . . - . . - . . - . . - . . - . . - . 以下是我目前编写的方法头: public void drawPatch(int width, int height, char primary, char secondary) { } 如果有人能把我引向正确的方向,我将非常感激 谢谢。在两个方向(垂直和水平)循环,然后在水平方向检查当前位置是偶数还是奇数,并相

我必须创建一个方法,它将在偶数列上打印主字符,在奇数列上打印次字符

下面是所需的输出

. - . 
. - . 
. - . 
. - . 
. - . 
. - . 
. - .
以下是我目前编写的方法头:

public void drawPatch(int width, int height, char primary, char secondary) {

}
如果有人能把我引向正确的方向,我将非常感激

谢谢。

在两个方向(垂直和水平)循环,然后在水平方向检查当前位置是偶数还是奇数,并相应地打印字符

public static void drawPatch(int width, int height, char primary, char secondary) {
    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            if ((i+1) % 2 == 0)
                System.out.print(primary);
            else
                System.out.print(secondary);
        }
        if (j < height - 1) { //go to next line except for last row.
            System.out.print("\n");
        }
    }
}
publicstaticvoiddrawpatch(int-width、int-height、char-primary、char-secondary){
对于(int j=0;j
欢迎使用堆栈溢出!请查看我们的,以帮助您提出一个好问题,从而得到一个好答案。提示:如果
col
是列号,
col%2==0
如果
col
是偶数,
col%2==1
如果
col
是奇数
%
是“模”运算符,以防您想研究它。非常感谢!