Java: 在打印这个金字塔形状的反向数字模式时遇到问题。

huangapple go评论69阅读模式
英文:

Java: Having trouble printing out this pyramid-like reversed number pattern

问题

for (int i = num; i >= 1; i--) 
{
    for (int j = num - i + 1; j <= num; j++) 
    { 
        System.out.print(j + " "); 
    } 
     
    System.out.println(); 
}

for (int i = 2; i <= num; i++)
{
    for (int j = num - i + 1; j <= num; j++)
    {
        System.out.print(j + " ");
    }
     
    System.out.println();
}

Note: The code provided above is the corrected version to achieve the desired pattern.

英文:

I need to know how to print out the following pattern:

5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
2 3 4 5 
3 4 5
4 5
5

Any and all help is appreciated.
What i have so far is this:

for (int i = 1; i &lt;= num; i++) 
        {
            for (int j = 1; j &lt;= i; j++) 
            { 
                System.out.print(j+&quot; &quot;); 
            } 
             
            System.out.println(); 
        }
	 for (int i = num-1; i &gt;= 1; i--)
			{
				for (int j = 1; j &lt;= i; j++)
				{
					System.out.print(j+&quot; &quot;);
				}
				 
				System.out.println();
			}

and it outputs this:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

So I have the structure of the pattern itself understood, but it seems that I need to reverse the flow somehow. This is what I do not understand.

答案1

得分: 2

更改循环条件如下所示:

public class Main {
    public static void main(String[] args) {
        int num = 5;
        for (int i = num; i >= 1; i--) {// 从 num 开始向下循环到 1
            for (int j = i; j <= num; j++) {// 从 i 开始向上循环到 num
                System.out.print(j + " ");
            }

            System.out.println();
        }
        for (int i = 2; i <= num; i++) {// 从 2 开始向上循环到 num
            for (int j = i; j <= num; j++) {// 从 i 开始向上循环到 num
                System.out.print(j + " ");
            }

            System.out.println();
        }
    }
}

输出:

5 
4 5 
3 4 5 
2 3 4 5 
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 
英文:

Change the loop conditions as shown below:

public class Main {
	public static void main(String[] args) {
		int num = 5;
		for (int i = num; i &gt;= 1; i--) {// Start with num and go downwards up to 1
			for (int j = i; j &lt;= num; j++) {// Start with i and go upwards up to num
				System.out.print(j + &quot; &quot;);
			}

			System.out.println();
		}
		for (int i = 2; i &lt;= num; i++) {// Start with 2 and go downwards up to num
			for (int j = i; j &lt;= num; j++) {// Start with i and go downwards up to num
				System.out.print(j + &quot; &quot;);
			}

			System.out.println();
		}
	}
}

Output:

5 
4 5 
3 4 5 
2 3 4 5 
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 

答案2

得分: 1

已接受的答案没有输出正确的模式,所以这段代码是有效的。

有两个循环,每个循环逐行迭代。第一个循环从5到1,第二个循环从2到5。

在每次迭代(每行)中,将在第一个数字旁边打印后续数字。

int num = 5;
for(int i = num ; i > 0 ; i--){
    System.out.print(i+" "); // 打印行的第一个数字
    for(int j = 1; j <= num-i; j++){
    // 根据行的需要打印额外的数字(第一行 = 0个额外数字,
    // 第二行 = 1个额外数字...)
        System.out.print((i+j)+" ");
    }
    System.out.println();  // 换行
}
for (int i = 2; i <= num; i++) { // 打印第二部分,从2开始
    for (int j = i; j <= num; j++) { // 打印额外数字
        System.out.print(j+" ");
    }
    System.out.println(); // 换行
}

输出结果如预期:

5 
4 5 
3 4 5 
2 3 4 5 
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 
英文:

Accepted answer not output the corrent pattern so... this code works.

Ther are two loops, each one iterate line by line. First from 5 to 1, the second one from 2 to 5.

In every iteration (every line) will print next to the first number the following numbers.

int num = 5;
for(int i = num ; i &gt; 0 ; i--){
    System.out.print(i+&quot; &quot;); //Print the first number of the line
    for(int j = 1; j &lt;= num-i; j++){
    //Print as extra number as line need (first line = 0 extra numbers, 
    //second line = 1 extra number...)
        System.out.print((i+j)+&quot; &quot;);
    }
    System.out.println();  //New line
}
for (int i = 2; i &lt;= num; i++) { //Print second part starting by 2
    for (int j = i; j &lt;= num; j++) { //Print extra numbers
        System.out.print(j+&quot; &quot;);
    }
    System.out.println(); //New line
}

And the output is as expected:

5 
4 5 
3 4 5 
2 3 4 5 
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 

huangapple
  • 本文由 发表于 2020年10月19日 06:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64418887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定