/**
 * @author HulinSciece
 * 5.17
 * 提示用户输入1 to 15 之间的数,打印输出金字塔图案。         1
 *                                        2  1  2            
 *                                     3  2  1     2    3
 * 分析:1.先写出左半边图案。  2.输出个数等于行数 number = lines 3.输出顺序是从大到小   4.空格数输出spaceNumber = (lines - 1)

 *        2  * space
 *        2.用格式化输出空格和数字    system.out.printf("%4s","")  and System.out.printf("%4d",i)
 *        3.写成方法方便其他程序调用
 *  the programe test passing
 *  
 * data:2019/01/02
 * 
 */


package com.Five.exercise;
import java.util.Scanner;
public class OutputPyramid {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //create a scanner
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter the lines:");
        int numberOfLines = input.nextInt();
        
        System.out.println("The pyramid is :");
        
        OutputPyramid(numberOfLines);
        
        
    }

    private static void OutputPyramid(int numberOfLines) {
        // TODO Auto-generated method stub
        for (int line = 1; line <= numberOfLines; line++) {
                    
                    //putout row number
                    int spaceNumber = 0;
        //            spaceNumber = (numberOfLines - line);
        //            String space = " ";
        //            System.out.printf("%" + spaceNumber + "s",space);
                    //putout space
                    //the numberOfLines > 9 
                    for (int i = numberOfLines-line; i > 0; i--) {
                        System.out.printf("%4s","");
                    
                    }
                    //putout the left number in the same line
                    for (int leftNumber = line; leftNumber > 0 ; leftNumber--) {
                        //using With formatted output, the number of changes automatically leave space
                        // not
                        System.out.printf("%4d",leftNumber); 
                    }
                    //putout the right number in the same
                    for (int rightNumber = 2; rightNumber <= line; rightNumber++) {
                        System.out.printf("%4d",rightNumber);
                    }
                    System.out.println();
                }
            }

}
 

java数字金字塔输出

相关文章:

  • 2022-12-23
  • 2021-08-16
  • 2022-12-23
  • 2020-07-17
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2022-12-23
  • 2021-07-14
  • 2022-12-23
相关资源
相似解决方案