ゞ沉默是金ゞ

          魚離不開水,但是沒有說不離開哪滴水.
          posts - 98,comments - 104,trackbacks - 0
          先看效果:


          代碼:

          public class PrintTriangle {
              
              
          public static void main(String[] args) {
                  System.out.println(
          "======左邊正三角形======");
                  printTopLeft(
          5);
                  System.out.println(
          "======左邊倒三角形======");
                  printDownLeft(
          5);
                  System.out.println(
          "======打印右正三角形======");
                  printTopRight(
          5);
                  System.out.println(
          "======打印右倒三角形======");
                  printDownRight(
          5);
                  System.out.println(
          "======打印正中三角形======");
                  printTopCenter(
          5);
                  System.out.println(
          "======打印倒中三角形======");
                  printDownCenter(
          5);
                  System.out.println(
          "======打印中正空三角形======");
                  printTopMidBlank(
          5);
                  System.out.println(
          "======打印中倒空三角形======");
                  printDownMidBlank(
          5);
                  System.out.println(
          "======打印正人字狀======");
                  printTopRen(
          5);

              }

              
          /*
               * 打印正人字狀
               
          */

              
          public static void printTopRen(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = i; j < n; j ++){
                          System.out.print(
          "   ");
                      }

                      
          //左半部分
                      for(int j = 1; j <= i; j++){
                          
          if(j == 1)
                              System.out.print(
          " * ");
                          
          else
                              System.out.print(
          "   ");
                      }

                      
          //右半部分
                      for(int j = 1; j < i; j++){
                          
          if(j == i - 1)
                              System.out.print(
          " * ");
                          
          else 
                              System.out.print(
          "   ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印倒空三角形
               
          */

              
          public static void printDownMidBlank(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = 1; j < i; j ++){
                          System.out.print(
          "   ");
                      }

                      
          for(int j = i; j <= n; j ++){
                          
          if(i == 1 || j == i|| i == n)
                              System.out.print(
          " * ");
                          
          else
                              System.out.print(
          "   ");
                      }

                      
          for(int j = i; j < n; j ++){
                          
          if(i == 1 || j == n - 1)
                              System.out.print(
          " * ");
                          
          else
                              System.out.print(
          "   ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印中正空三角形
               
          */

              
          public static void printTopMidBlank(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = i; j < n; j ++){
                          System.out.print(
          "   ");
                      }

                      
          //左半部分
                      for(int j = 1; j <= i; j++){
                          
          if(j == 1 || i == n)
                              System.out.print(
          " * ");
                          
          else
                              System.out.print(
          "   ");
                      }

                      
          //右半部分
                      for(int j = 1; j < i; j++){
                          
          if(j == i - 1 || i == n)
                              System.out.print(
          " * ");
                          
          else 
                              System.out.print(
          "   ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印倒中三角形
               
          */

              
          public static void printDownCenter(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = 1; j < i; j ++){
                          System.out.print(
          "   ");
                      }

                      
          for(int j = i; j <= n; j ++){
                          System.out.print(
          " * ");
                      }

                      
          for(int j = i; j < n; j ++){
                          System.out.print(
          " * ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印正中三角形
               
          */

              
          public static void printTopCenter(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = i; j < n; j ++){
                          System.out.print(
          "   ");
                      }

                      
          //左半部分
                      for(int j = 1; j <= i; j++){
                          System.out.print(
          " * ");
                      }

                      
          //右半部分
                      for(int j = 1; j < i; j++){
                          System.out.print(
          " * ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印右倒三角形
               
          */

              
          public static void printDownRight(int n){
                  
          for(int i = n; i >= 1; i --){
                      
          for(int j = n; j > i; j --){
                          System.out.print(
          "   ");
                      }

                      
          for(int j = i; j >= 1; j --){
                          System.out.print(
          " * ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印右正三角形
               
          */

              
          public static void printTopRight(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = n; j > i; j --){
                          System.out.print(
          "   ");
                      }

                      
          for(int j = i; j >= 1; j--){
                          System.out.print(
          " * ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印左正三角型
               
          */

              
          public static void printTopLeft(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = 1; j <= i; j ++){
                          System.out.print(
          " * ");
                      }

                      System.out.println();
                  }

              }

              
          /*
               * 打印左倒三角形
               
          */

              
          public static void printDownLeft(int n){
                  
          for(int i = 1; i <= n; i ++){
                      
          for(int j = n; j >= i; j --){
                          System.out.print(
          " * ");
                      }

                      System.out.println();
                  }

              }

          }

          希望大家多提意見。
          posted on 2009-06-02 14:43 ゞ沉默是金ゞ 閱讀(1409) 評論(2)  編輯  收藏 所屬分類: Java SE

          FeedBack:
          # re: 用 * 打印出各種圖形
          2009-06-02 15:03 | FEJAY
          學習了   回復  更多評論
            
          # re: 用 * 打印出各種圖形
          2009-06-03 17:14 | thebye85
          看得我都是星星  回復  更多評論
            
          主站蜘蛛池模板: 曲阳县| 柯坪县| 黄龙县| 新干县| 务川| 抚远县| 玉树县| 泸州市| 宣化县| 永济市| 奈曼旗| 公安县| 武汉市| 盐亭县| 新泰市| 淮北市| 南靖县| 辽源市| 万荣县| 措美县| 鹤壁市| 泰来县| 镇宁| 偃师市| 开鲁县| 曲靖市| 兖州市| 寿宁县| 西乡县| 德惠市| 保定市| 曲水县| 垦利县| 定结县| 鲜城| 农安县| 泰州市| 云龙县| 玉门市| 疏勒县| 临夏县|