嵌套 For 循环方块 - Java

Nested For Loop Square- Java

提问人:isabel8998 提问时间:6/10/2023 最后编辑:Pshemoisabel8998 更新时间:6/11/2023 访问量:52

问:

我是 Java 的新手,我正在使用嵌套的 for 循环制作两个三角形的正方形。它应该看起来像这样:

+ + + + + 
* + + + + 
* * + + + 
* * * + + 
* * * * + 

到目前为止,我的代码如下所示:

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

但它给我的只是广场的星形部分。我不知道如何从这里开始。谁能帮忙?

Java for 循环 嵌套

评论


答:

1赞 azro 6/10/2023 #1

添加星星后,您可以添加加号i5-i

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

可用于简化代码String.repeat

int lineLength = 5;
for (int i = 0; i < lineLength; i++) {
    System.out.println("* ".repeat(i) + "+ ".repeat(lineLength - i));
}
0赞 alexwbt 6/10/2023 #2
for (int x = 0; x < 5; x++) {
    for (int y = 0; y < 5; y++) {
        System.out.print(x > y ? "* " : "+ ");
    }
    System.out.println();
}

你可能会想要这样的东西吗?

评论

1赞 azro 6/10/2023
请不要给出无解的答案
0赞 azro 6/10/2023
您是如何在 7 年前创建帐户的,现在才使用它?这是非常罕见的
0赞 Reilas 6/11/2023 #3

您可以利用 Arrays#fill 方法预填充 char 的二维数组。

然后,迭代以将相应的索引替换为星号。

char[][] characters = new char[5][5];
for (char[] row : characters) Arrays.fill(row, '+');
int count = 0;
for (char[] row : characters) {
    for (int index = 0; index < count; index++)
        row[index] = '*';
    count++;
}
int index = 0;
for (char[] row : characters) {
    for (char character : row) {
        if (index++ != 0) System.out.print(" ");
        System.out.print(character);
    }
    index = 0;
    System.out.println();
}

输出

+ + + + + 
* + + + + 
* * + + + 
* * * + + 
* * * * + 

此外,还可以使用允许输入值的方法。

void draw(int size, char charA, char charB) {
    char[][] characters = new char[size][size];
    for (char[] row : characters) Arrays.fill(row, charB);
    int count = 0;
    for (char[] row : characters) {
        for (int index = 0; index < count; index++)
            row[index] = charA;
        count++;
    }
    int index = 0;
    for (char[] row : characters) {
        for (char character : row) {
            if (index++ != 0) System.out.print(" ");
            System.out.print(character);
        }
        index = 0;
        System.out.println();
    }
}

示例用法和输出。

draw(20, '@', '-');
- - - - - - - - - - - - - - - - - - - -
@ - - - - - - - - - - - - - - - - - - -
@ @ - - - - - - - - - - - - - - - - - -
@ @ @ - - - - - - - - - - - - - - - - -
@ @ @ @ - - - - - - - - - - - - - - - -
@ @ @ @ @ - - - - - - - - - - - - - - -
@ @ @ @ @ @ - - - - - - - - - - - - - -
@ @ @ @ @ @ @ - - - - - - - - - - - - -
@ @ @ @ @ @ @ @ - - - - - - - - - - - -
@ @ @ @ @ @ @ @ @ - - - - - - - - - - -
@ @ @ @ @ @ @ @ @ @ - - - - - - - - - -
@ @ @ @ @ @ @ @ @ @ @ - - - - - - - - -
@ @ @ @ @ @ @ @ @ @ @ @ - - - - - - - -
@ @ @ @ @ @ @ @ @ @ @ @ @ - - - - - - -
@ @ @ @ @ @ @ @ @ @ @ @ @ @ - - - - - -
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ - - - - -
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ - - - -
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ - - -
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ - -
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ -