Java 嵌套用于以数组样式打印输出的循环

Java 嵌套用于以数组样式打印输出的循环,java,for-loop,Java,For Loop,我正在尝试学习嵌套For循环,我希望它能用*打印出长度和宽度的显示。在宽度为3、长度为4的情况下,所需的输出应类似于由*构成的3x4框 我的For循环嵌套是否正确?如果宽度值大于长度,是否需要两个For循环 int width, length, i, j; width = 3; length = 4; //Print the output System.out.pr

我正在尝试学习嵌套For循环,我希望它能用*打印出长度和宽度的显示。在宽度为3、长度为4的情况下,所需的输出应类似于由*构成的3x4框

我的For循环嵌套是否正确?如果宽度值大于长度,是否需要两个For循环

int width, length, i, j;

                width = 3;
                length = 4;

                //Print the output
                System.out.print("Here are the stars: ");
                System.out.print("\n");
                for(i=0; i<width; i++) {
                    System.out.print("*");
                    for(j=0; j<length; j++) {
                        System.out.println("*");
                    }
                }
            }
       }

您可以指定一个3x4星形图案,通常表示3行4列。但显然你想要4行3列。也就是说,长度是行数3,宽度4是列数

for(int row = 0; row < length; row++) {
    for(int col = 0; col < width; col++) {
        System.out.print("*");
    }
    System.out.println();
}

作为旁注,没有理由在for循环之外声明i,j,事实上这是一种糟糕的风格。在上面的代码片段中,我在各自的for循环中声明了row和col变量。

您需要做两个小修改。将内部循环println更改为print,然后只需在内部循环后添加println,如下所示:

    int width, length, i, j;

    width = 3;
    length = 4;

    //Print the output
    System.out.print("Here are the stars: ");
    System.out.print("\n");
    for(i=0; i<length; i++) {
        System.out.print("*");
        for(j=0; j<width; j++) {
            System.out.print("*");
        }
        System.out.println("");
    }
另外,您需要更改宽度=2和长度=3,或者从1开始循环。当前实现打印一个4-5矩阵。

原始代码 因为您在内部循环中的每一行后面都添加了一个换行符。将println移动到内部循环之后。外部循环需要做的就是控制内部循环的执行次数并打印换行符。 另外,在内部循环之前打印一个错误的

这是你需要的

    // Print the output
    System.out.println("Here are the stars:");
    for (i = 0; i < width; i++) {
        for (j = 0; j < length; j++) {
            System.out.print("*");
        }
        System.out.println();
    }

此解决方案在一个内环中打印长度星,后跟一个换行符。它的宽度是这个倍数。

您不能在第一个循环中打印星号。这是错误的:

for(i=0; i<width; i++) {
                System.out.print("*");
下面是一个更正:

int width, length, i, t;

    width = 3;
    length = 4;

    //Print the output
    System.out.print("Here are the stars: ");
    System.out.print("\n");
    for (i = 0; i < width; i++) {
        for (t = 0; t < length; t++) {
            System.out.print("*");
        }
        System.out.println();
    }

基本上,这里正在进行的是,它正在通过并逐行打印星星。我切换了宽度和长度,因为它应该转到每一行,并逐行打印出星星

int width, length;

    width = 3;
    length = 4;

    //Print the output
    System.out.print("Here are the stars: ");
    System.out.print("\n");
    for (int i = 0; i < length; i++) {
        for (int j = 0; j < width; j++) {
            System.out.print("*");
        }
        System.out.println();
    }

这是我的尝试,因为我看到每个人都在切换宽度和长度?我认为长度意味着高度

int width = 3;
int length = 4;

System.out.println("Here are the stars: ");

for(int i = 0; i < length; i++) {
    for(int j = 0; j < width; j++) {
        System.out.print("*");
    }
    System.out.println();
}

一旦确定了正确的逻辑,您还需要考虑性能。输出到资源是有代价的。我会分割阵列,尽量减少对System.out的写入

StringBuffer sb = new StringBuffer(); 
for (int w = 0; w < width; w++) {   
    sb.append('*'); 
}

final String s = sb.toString(); 
for (int h = 0; h < height; h++) { 
    System.out.println(s); 
}

您得到的输出是什么?这是你想要的还是不同的?如果不同,预期输出是什么?请发帖。我本来打算回答的,但是每个人都被否决了:从逻辑上讲,你应该从上到下,然后从左到右,所以第一个循环使用长度,然后内部循环使用宽度。然后你需要处理何时打印新行。你提到3x4是你想要打印的星星图案。这通常意味着3行4列,但显然你的意思是相反的。实际上没有必要在外部声明i和j。按照Java内存管理的工作方式,只在for中声明会更快。这两个降级投票者中的任何一个会费心评论吗?目前不会编译,您不会将i或j声明为int。您需要注意不要在外部声明它们,但您在内部操作失败。@cricket_007我提供了错误代码部分的修改版本。OP中的代码和所有答案中的代码也不会编译。例如,它不是在类中定义的方法中定义的。在解释操作码的错误以及解决方法时,我添加了一些解释。@Blip我已经添加了一些解释。t在内部循环中应该是j。是的,如果t和j在循环中,速度会更快。我不知道我可以在循环的第一部分打印新行,这才是最让我困惑的。谢谢你教了我一些新的东西:帮助了很多人。那么,为什么这是有效的,我设置我的宽度循环从1开始,正如你所说的,我得到了我想要的显示器。你能给我讲讲逻辑吗?如果将宽度设置为3,并在0处开始循环,将循环运行4次,即0、1、2、3。因此,为了获得3x4,有两种选择。设置宽度=2并在0,1,2处开始循环,或者设置宽度=3并在1,2,3处开始循环。长度的逻辑类似。这有意义吗?谢谢,是的,我刚想到我应该先知道长度,然后再知道宽度。感谢您的解释:是的,长度确实意味着高度,这个解决方案也适用。非常感谢你!。我从大家的解释中学到了很多:
int width = 3;
int length = 4;

System.out.println("Here are the stars: ");

for(int i = 0; i < length; i++) {
    for(int j = 0; j < width; j++) {
        System.out.print("*");
    }
    System.out.println();
}
Here are the stars: 
***
***
***
***
StringBuffer sb = new StringBuffer(); 
for (int w = 0; w < width; w++) {   
    sb.append('*'); 
}

final String s = sb.toString(); 
for (int h = 0; h < height; h++) { 
    System.out.println(s); 
}