Java列印M圖形(二維數組)——(九)

來源:互聯網
上載者:User

標籤:can   uvc   tcp   jmm   stl   mfp   rbm   ieee   pgm   

對於平面圖形輸出集合圖形與數字組合的,用二維數組。先在Excel表格中分析一下,找到簡單的規律。二維數組的行數為行高,列數為最後一個數大小。

對於減小再增大再減小再增大的,可以用一個boolean標誌其是增加還是減小狀態,減到最小時將其標誌設為相反的並改變這時的初值大小。

效果:

 

Excel中分析

 

Java代碼:
 1 package Java_Test; 2  3 public class PrintM { 4  5     /* 6       7         3     7 8        2  4  6  8 9       1    5      910       平面圖形(二維數組)11       12      */13     public static void main(String[] args) {14         15         int atr[][]=new int[3][9];16 //        產生9個數,放入對應位置17         boolean flag=false;   //false代表橫座標在減小,true代表在增大18 //        初始位置19         int x=2;20         int y=0;21         for(int i=1;i<=9;i++){22             23 //            放入第一個數24             atr[x][y]=i;25 //            y始終在增大26             y++;27             28             if(!flag){  //如果x在減小讓其自減29                 x--;30             }31             32             if(flag){33                 x++;34             }35 36             if(x<0){  //x減到0,再減為-137                 flag=true;38                 x=x+2;39             }40             41             if(x>2){  //x加2,再加為342                 flag=false;43                 x=x-2;44             }45         }46         47         48         for(int i=0;i<3;i++){49             for(int j =0;j<9;j++){50                 if(atr[i][j]==0){51                     System.out.print(" ");52                 }else{53                     System.out.print(atr[i][j]);54                 }55             }56             System.out.println();57         }58         59         60         61     }62 }

 

測試:

 

 

-------------------------------------------------------輸入n個數輸出M的改造--------------------------------
 1 package Java_Test; 2  3 import org.junit.Test; 4  5 public class PrintM_duogeshu { 6  7     /* 8      *  9      * 3 7 2 4 6 8 1 5 9 平面圖形(二維數組)10      * 11      */12     public static void test1(int num) {13 14         int height = (num / 4) + 1; // 4條邊15 16         int atr[][] = new int[height][num];17         // 產生9個數,放入對應位置18         boolean flag = false; // false代表橫座標在減小,true代表在增大19         // 初始位置20         int x = height-1;21         int y = 0;22         for (int i = 1; i <= num; i++) {23 24             // 放入第一個數25             atr[x][y] = i;26             // y始終在增大27             y++;28 29             if (!flag) { // 如果x在減小讓其自減30                 x--;31             }32 33             if (flag) {34                 x++;35             }36 37             if (x < 0) { // x減到0,再減為-138                 flag = true;39                 x = x + 2;40             }41 42             if (x > height-1) { // x加2,再加為343                 flag = false;44                 x = x - 2;45             }46         }47 48         for (int i = 0; i < atr.length; i++) {49             for (int j = 0; j < atr[i].length; j++) {50                 if (atr[i][j] == 0) {51                     System.out.print(" ");52                 } else {53                     System.out.print(atr[i][j]);54                 }55             }56             System.out.println();57         }58 59     }60     61     @Test62     public void test(){63         PrintM_duogeshu.test1(13);64     }65 }

 

測試:

 

Java列印M圖形(二維數組)——(九)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.