 1.1
1.1               1.2
  1.2从图中,我们可以发现:第1行的空格为4个,第2行是3个,第3行是2个,……,每行依次递减,直至最后一行空格数为0;而星号数目是第1行是1个,第2行是3,第3行是5,……,每行依次递增2,直至最后一行星号数为9。总结数据,我们可以得到表1.1所示的规律。
| 行数 | 空格数 | 星星数 | ||
| 1 | 4 | 5-1 | 1 | 1*2-1 | 
| 2 | 3 | 5-2 | 3 | 2*2-1 | 
| 3 | 2 | 5-3 | 5 | 3*2-1 | 
| 4 | 1 | 5-4 | 7 | 4*2-1 | 
| 5 | 0 | 5-5 | 9 | 5*2-1 | 
| 规律 | 依次减1 | 5-行数 | 依次加2 | 行数*2-1 | 
根据图中我们可以发现这种规律,那么接下来是不是就简单了。
3,确定空格数
由于每行空格数有着“5–行数”的规律。所以在第i行的时候,空格数就为5–i。所以我们只要把5–i个空格打印出来即可。
| 行数 | 空格数 | 星星数 | ||
| 1 | 0 | 1-1 | 9 | 5*2-1 | 
| 2 | 1 | 2-1 | 7 | 4*2-1 | 
| 3 | 2 | 3-1 | 5 | 3*2-1 | 
| 4 | 3 | 4-1 | 3 | 2*2-1 | 
| 5 | 4 | 5-1 | 1 | 1*2-1 | 
| 规律 | 依次递增1 | 行数-1 | 依次递减2 | 行数*2-1(反向) | 
package com.javase.demo;
import java.util.Scanner;
    /**
    * 金字塔
    * @author Mr.Zhang
    *
    */
    public class Pyramid {
      static Scanner input = new Scanner(System.in);
    /**
    * *****打印金字塔*****
    * 1,确定金字塔行数
    * 2,确认空格数
    * 3,确认星星数
    * @param args
    */
    public static void main(String[] args) {
      entrance();
    }
    /**
    * 入口项
    */
    public static void entrance() {
      System.out.println("请选择(0--正金字塔,1--倒金字塔,2--菱形金字塔)");
      String select = input.nextLine();
      if(isNumber(select)){
        int selectInt = Integer.parseInt(select);
        switch(selectInt){
          case 0:
            uprightPyramid();
            break;
          case 1:
            fallPyramid();
            break;
          case 2:
            System.out.println("该功能尚未完善!");
            break;
          default:
            System.out.println("请输入正确的选项!");
            entrance();
            break;
        }
      }else if(!select.equals(0) || !select.equals(1) || !select.equals(2)){
        nullSuccess();
      }
    }
    /**
    * 打印正金字塔
    * @param input
    */
    public static void uprightPyramid() {
      System.out.println("请输入行数:");
      String row = input.nextLine();
      if(isNumber(row)){
        int rows = Integer.parseInt(row);
        for(int i = 1;i <= rows;i++){ //循环输入的行数,
          for(int j = 1;j <= rows - i;j++){ //输出循环每行的空格
            System.out.print(" ");
          }
          for(int k = 1;k <= 2 * i - 1;k++){ // 输出循环每行的★
            System.out.print("★");
          }
          System.out.println();
        }
          System.out.println("打印完成,线程结束");
      }else{
        nullSuccess();
      }
    }
    /**
    * 打印倒金字塔
    */
    public static void fallPyramid(){
      System.out.println("请输入行数:");
      String row = input.nextLine();
      if(isNumber(row)){
        int rows = Integer.parseInt(row);
        for(int i = rows;i >= 1;i--){
          for(int j = 0;j < rows-i;j++){ //打印空格数
            System.out.print(" ");
          }
          for(int k = 0;k < i * 2 - 1;k++){ //打印★数
            System.out.print("★");
          }
          System.out.println();
        }
        System.out.println("打印完成,线程结束");
      }else{
        nullSuccess();
      }
    }
    /**
    * 判断是否为数字
    * @param str
    * @return
    */
    public static boolean isNumber(String str){
        boolean ok = false;
        if(null != str && str.matches("^[0-9]+$")){
          return true;
        }
      return ok;
    }
    /**
    * 调用错误结果
    */
    public static void nullSuccess(){
      System.out.println("您输入的不是数字,一遍浪去,不听话的孩子!");
    }
}
原文:http://www.cnblogs.com/huangzh1996/p/7162636.html