[英]How to join the output of 2 nested loops next to each other?
So I'm currently working on a personal project and I made a program that prints out star patterns.所以我目前正在做一个个人项目,我制作了一个打印星形图案的程序。 On one of the star patterns I want this output:在我想要这个输出的星形图案之一上:
Figure数字
* *
** **
*** ***
**** ****
**********
So I made one method print out this:所以我做了一种方法打印出来:
Figure 1图1
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
System.out.println("");
}
*
**
***
****
*****
And another method print out this:另一种方法打印出这个:
Figure 2图2
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i); j >= 0; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
*
**
***
****
*****
My question: How can I put the first method stars next to the other method stars?我的问题:如何将第一个方法星放在其他方法星旁边?
This is what I got so far:这是我到目前为止得到的:
public static void printStarsVictory(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*" + " ");
}
System.out.println("");
}
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i); j >= 0; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
This is what is printing so far:这是目前正在打印的内容:
*
**
***
****
*****
*
**
***
****
*****
Any idea how to solve this?知道如何解决这个问题吗?
I think you are on the right track you just need to combine your two programs into the inner for loop:我认为您走在正确的轨道上,您只需要将两个程序组合到内部 for 循环中:
private static void printStarsVictory(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n * 2; j++) {
if (j <= i || j >= (n * 2 - i)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Example Usage printStarsVictory(5)
:示例用法printStarsVictory(5)
:
* *
** **
*** ***
**** ****
*********
Example Usage printStarsVictory(12)
:示例用法printStarsVictory(12)
:
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
******** ********
********* *********
********** **********
*********** ***********
***********************
Well, System.out.println() prints only to the next row, not to the right.好吧, System.out.println() 只打印到下一行,而不是右边。 The only way is to create a new algorithm.唯一的方法是创建一个新的算法。 You should put everything into single loop.您应该将所有内容都放入单个循环中。
// loop starts from 0 and user counts from 1, so we wil decrement it by 1
n--;
// this loops moves pointer vertically
for (int i = 0; i < n; i++) {
// this loops moves pointer horisontally
for (int j = 0; j < n*2; j++) {
// here we choose what to print
if (j <= i || j >= n*2-i) {
System.out.print("*");
} else System.out.print(" ");
}
System.out.println();
}
Each row has the increasing number of stars and decreasing number of spaces.每行的星数增加,空格数减少。
eg For n=5
first row has 2 stars at each side and 8 spaces.例如,对于n=5
第一行每边有 2 颗星和 8 个空格。
At each iteration you can increase stars and decrease spaces and print them on the same line:在每次迭代中,您可以增加星星并减少空格并将它们打印在同一行上:
public static void printStarsVictory(int n) {
int sp = n * 2 - 2; // number of spaces
for (int i = 1; i <= n; i++) {
printStars(i); // print stars on the left side
int temp = sp;
while (temp > 0) {
System.out.print(" ");
temp--;
}
printStars(i); // // print stars on the right side
System.out.println("");
sp -= 2; // decrease spaces on each side
}
}
public static void printStars(int i) {
while(i>0) {
System.out.print("*");
i--;
}
}
Can you keep them in an array before printing to output:您可以在打印到输出之前将它们保存在数组中吗:
public static void printStarsVictory(int n) {
StringBuilder[] lines = new StringBuilder[n + 1];
for (int i = 0; i < n; i++) {
lines[i] = new StringBuilder();
for (int j = 0; j <= i; j++) {
lines[i].append("*" + " ");
}
for (int j = 2 * (n - i - 1); j > 0; j--) {
lines[i].append(" ");
}
}
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i - 1); j > 0; j--) {
lines[i].append(" ");
}
for (int j = 0; j <= i; j++) {
lines[i].append("* ");
}
}
for (StringBuilder line : lines) {
System.out.println(line);
}
}
You can visualize this figure as a matrix of numbers in a range: [-n, 0]
inclusive vertically and [-n, n]
inclusive horizontally, where each point is:您可以将此图可视化为一个范围内的数字矩阵: [-n, 0]
包括垂直和[-n, n]
包括水平,其中每个点是:
m[i][j] = Math.abs(i) - Math.abs(j);
If n = 4
, then this matrix looks like this:如果n = 4
,则该矩阵如下所示:
0 1 2 3 4 3 2 1 0
-1 0 1 2 3 2 1 0 -1
-2 -1 0 1 2 1 0 -1 -2
-3 -2 -1 0 1 0 -1 -2 -3
-4 -3 -2 -1 0 -1 -2 -3 -4
int n = 4;
IntStream.rangeClosed(-n, 0)
.map(Math::abs)
.peek(i -> IntStream.rangeClosed(-n, n)
.map(Math::abs)
.mapToObj(j -> i > j ? " " : "* ")
.forEach(System.out::print))
.forEach(i -> System.out.println());
Output:输出:
* *
* * * *
* * * * * *
* * * * * * * *
* * * * * * * * *
See also: Making an hourglass using asterisks in java另请参阅: 在 Java 中使用星号制作沙漏
Similar to this previous answer , you can use two nested for loops and one if else statement in the same way:与上一个答案类似,您可以以相同的方式使用两个嵌套的for 循环和一个if else 语句:
public static void main(String[] args) {
int n = 9;
for (int i = -n; i <= 0; i++) {
for (int j = -n; j <= n; j++)
if (Math.abs(i) <= Math.abs(j)
// in chessboard order
&& (i + j) % 2 == 0
// vertical borders
|| Math.abs(j) == n)
System.out.print("*");
else
System.out.print(" ");
System.out.println();
}
}
Output:输出:
* *
** **
* * * *
** * * **
* * * * * *
** * * * * **
* * * * * * * *
** * * * * * * **
* * * * * * * * * *
** * * * * * * * **
See also:也可以看看:
• How to print a given diamond pattern in Java? •如何在Java 中打印给定的菱形图案?
• Empty diamond shape with numbers •带有数字的空菱形
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.