Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/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_Loops_Printing - Fatal编程技术网

Java 加号模式的水平重复

Java 加号模式的水平重复,java,loops,printing,Java,Loops,Printing,我必须生成以下模式: 到目前为止,我已经: public class Pattern { public static void main(String[] args) { final char indentChar = ' '; final char fillChar = '+'; int fillWidth; int indentWidth; int triangleBaseLength = 9; int triangleHeight =

我必须生成以下模式:

到目前为止,我已经:

public class Pattern {

public static void main(String[] args) {

    final char indentChar = ' ';
    final char fillChar = '+';
    int fillWidth;
    int indentWidth;
    int triangleBaseLength = 9;
    int triangleHeight = (triangleBaseLength / 2) + 1;


    int horizontalRepeats = 5;
    int verticalRepeats = 2;

     for (int y = 1; y <= verticalRepeats; y++) { //vertical Repeats                   

           for (int i = 0; i < triangleHeight; i++) { //Top downwards                                                    
                 for (indentWidth = 0; indentWidth < triangleHeight * 0 + i; indentWidth++) {      
                       System.out.print(indentChar);

                }

                 for (fillWidth = 0; fillWidth < (triangleBaseLength - i * 2); fillWidth++) {               
                       System.out.print(fillChar);

                }

                 System.out.println();

                }

                 for (int i = 1; i <= triangleHeight; i++) { //Top upwards                                                    
                   for (indentWidth = 0; indentWidth < triangleHeight * 1 - i; indentWidth++) {                
                         System.out.print(indentChar);

                   }

                   for (fillWidth = 0; fillWidth < (i * 2 - 1); fillWidth++) {                                

                         System.out.print(fillChar);


                   }

                    System.out.println(); //vertical Repeats

                }
}

模式中有一些重复的行也是一个问题,但目前没有高优先级。 我的问题是在水平面上重复这个模式

这是我目前掌握的代码:

public class Pattern {

public static void main(String[] args) {

    final char indentChar = ' ';
    final char fillChar = '+';
    int fillWidth;
    int indentWidth;
    int triangleBaseLength = 9;
    int triangleHeight = (triangleBaseLength / 2) + 1;


    int horizontalRepeats = 5;
    int verticalRepeats = 2;

     for (int y = 1; y <= verticalRepeats; y++) { //vertical Repeats                   

           for (int i = 0; i < triangleHeight; i++) { //Top downwards                                                    
                 for (indentWidth = 0; indentWidth < triangleHeight * 0 + i; indentWidth++) {      
                       System.out.print(indentChar);

                }

                 for (fillWidth = 0; fillWidth < (triangleBaseLength - i * 2); fillWidth++) {               
                       System.out.print(fillChar);

                }

                 System.out.println();

                }

                 for (int i = 1; i <= triangleHeight; i++) { //Top upwards                                                    
                   for (indentWidth = 0; indentWidth < triangleHeight * 1 - i; indentWidth++) {                
                         System.out.print(indentChar);

                   }

                   for (fillWidth = 0; fillWidth < (i * 2 - 1); fillWidth++) {                                

                         System.out.print(fillChar);


                   }

                    System.out.println(); //vertical Repeats

                }
}
公共类模式{
公共静态void main(字符串[]args){
最终字符indentChar='';
最终字符fillChar='+';
int填充宽度;
int缩进宽度;
int triangleBaseLength=9;
int trianglelight=(triangleBaseLength/2)+1;
int水平重复次数=5;
int垂直重复=2;

对于(int y=1;y首先,您还应该在
+
符号的右侧包含空格。这意味着,您必须为
循环添加另一个
,而不是为
循环打印每一行,而不是为
循环添加两个

其次,您需要将这3个
for
循环包装成另一个
for
循环,以便水平重复

for (int i = 0; i < triangleHeight; i++) { //Top downwards

    for (int h = 0; h < horizontalRepeats; h++) {
        for (indentWidth = 0; indentWidth < triangleHeight * 0 + i; indentWidth++) {
            System.out.print(indentChar);
        }

        for (fillWidth = 0; fillWidth < (triangleBaseLength - i * 2); fillWidth++) {
            System.out.print(fillChar);
        }

        // Repeat the first for loop...
        for (indentWidth = 0; indentWidth < triangleHeight * 0 + i; indentWidth++) {
            System.out.print(indentChar);
        }
    }

    System.out.println();
}
for(inti=0;i
然而,这并不能解决额外线路的问题。我会让你自己来解决