简体   繁体   English

显示倒金字塔

[英]Display upside-down pyramid

I want to have output like this 我想要这样的输出

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

How can I leave a space on left hand side ? 如何在左侧留出空间?

public class Exercise4_17 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the number of lines: ");
        int num = input.nextInt();
        Exercise4_17 exe = new Exercise4_17();
        exe.displayPyramid(num);
    }

    private void displayPyramid(int num) {

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

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

My output 我的输出

Enter the number of lines: 5
5 4 3 2 1 2 3 4 5 
4 3 2 1 2 3 4 
3 2 1 2 3 
2 1 2 
1 

Your code is very close. 您的代码非常接近。 First, as a matter of style, I would use the same variable name for the second loop ( j is jarring, just use k ). 首先,就样式而言,我将在第二个循环中使用相同的变量名( j令人讨厌,只需使用k )。 Second, and the real problem, you are modifying num in your loop. 其次,真正的问题,您正在修改num 你的循环。 Save the initial value before you loop and use that for your space calculation. 循环之前请保存初始值,并将其用于空间计算。 Like, 喜欢,

final int initial = num; // <-- add this
for (int i = num; i >= 1; i--) {
    for (int space = 1; space <= initial - i; ++space) { // <-- use initial instead of num
        System.out.print("  ");
    }
    for (int k = num; k >= 1; k--) {
        System.out.print(k + " ");
    }
    for (int k = 2; k <= num; k++) { // <-- j should still work of course...
        System.out.print(k + " ");
    }
    System.out.println();
    num--;
}

Hope this helps. 希望这可以帮助。

import java.util.Scanner;
public class MainClass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //Taking noOfRows value from the user
        System.out.println("How Many Rows You Want In Your Pyramid?");
        int noOfRows = sc.nextInt();
        //Initializing rowCount with noOfRows
        int rowCount = noOfRows;
        System.out.println("Here Is Your Pyramid");
        //Implementing the logic
        for (int i = 0; i < noOfRows; i++) {
            //Printing i*2 spaces at the beginning of each row
            for (int j = 1; j <= i*2; j++) {
                System.out.print(" ");
            }
            //Printing j where j value will be from 1 to rowCount
            for (int j = rowCount; j >= 1; j--) {
                 System.out.print(j+" ");
             }
             //Printing j where j value will be from rowCount-1 to 1
             for (int j = 2; j <= rowCount; j++) {
                    System.out.print(j+" ");
            }
            System.out.println();
            //Decrementing the rowCount
            rowCount--;
        }
    }
}

ref link 引用链接

Output: How Many Rows You Want In Your Pyramid? 输出:您想要在金字塔中排几行? 5 Here Is Your Pyramid 5这是你的金字塔

 5 4 3 2 1 2 3 4 5

    4 3 2 1 2 3 4 

     3 2 1 2 3 

       2 1 2 

         1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM