Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.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,我试图弄清楚如何使用这种逻辑使我的代码打印出正确的模式 Enter len:7 Enter repeat:3 \--\--\ --\--\- -\--\-- \--\--\ --\--\- -\--\-- \--\--\ Bye 这是我写的代码 for(int i = 0; i < len; i++) { for(int j = 0; j < len; j++) { if((j%repeat == 0))

我试图弄清楚如何使用这种逻辑使我的代码打印出正确的模式

Enter len:7
Enter repeat:3
\--\--\
--\--\-
-\--\--
\--\--\
--\--\-
-\--\--
\--\--\
Bye
这是我写的代码

for(int i = 0; i < len; i++)
    {
        for(int j = 0; j < len; j++)
        {
            if((j%repeat == 0))
                System.out.print("\\");
            else
                System.out.print("-");
        }

        System.out.println();
    }
    System.out.println("Bye");
我怎样才能让我的工作像这样?通过这些行我知道不同之处在于位置被一个位置关闭,但如何实现这一点是我的问题。

for(int I=0;Ifor(int i = 0; i < len; i++)
{
    for(int j = 0; j < len; j++)
    {
        if(((i+j)%repeat == 0))
            System.out.print("\\");
        else
            System.out.print("-");
    }

    System.out.println();
}
System.out.println("Bye");
{ 对于(int j=0;j i+j

这确实解决了问题,但它不能解释解决方案背后的原因。帮助人们解决同类问题的帖子更有价值。如果你能加上解释,这将大大增加这个答案的价值。
for(int i = 0; i < len; i++)
{
    for(int j = 0; j < len; j++)
    {
        if(((i+j)%repeat == 0))
            System.out.print("\\");
        else
            System.out.print("-");
    }

    System.out.println();
}
System.out.println("Bye");