java楊輝三角和空心菱形(二維數組篇)

來源:互聯網
上載者:User

標籤:二維   div   迴圈   oid   color   位元組   tin   span   else   

一、楊輝三角

 1 import java.util.Scanner;                                   //匯入包 2 public class Test7 { 3     public static void main(String[]args){ 4  5         Scanner row=new Scanner(System.in);                 //調用掃描器 6         int n = row.nextInt();                              //定義類型 7         int [][] a=new int[n][n];                           //定義二位元組 8         int i,j;                                            //聲明整型 9         10         for(i=0;i<n;i++){                                   //迴圈行11                         12                 for(j=0;j<=i;j++){                          //迴圈列13 14                     if(j==0 || j==i){                       //每行第一個和最後一個15                         a[i][j]=1;16                         17                         System.out.print(a[i][j]+"\t");18                     }19                     20                     else{21                         a[i][j]=a[i-1][j-1]+a[i-1][j];      //等於上一行同位置和前一位置兩個數之和22                         System.out.print(a[i][j]+"\t");23                     }                                            24                 }25                 System.out.println();                       //換行26                 System.out.println();27             28         }29     }30 31 }

 

二、空心菱形

public class Test6 {    public static void main(String[]args){        String [][]a=new String[11][];               //定義二維數組        for(int i=0;i<11;i++){                       //迴圈行            if(i<=5){                                //上半部分                for(int j=0;j<11;j++){               //迴圈列                    if(j==5-i || j==5+i){            //迴圈列                    System.out.print("*");           //輸出*號                    }                    else{                            //輸出其他恐怖部分                        System.out.print(" ");                    }                }                System.out.println();            }            else{                                    //下半部分                for(int j=0;j<11;j++){               //迴圈行                    if(j==i-5 || j==(10-(i-5))){    //迴圈列                        System.out.print("*");                    }                    else{                        System.out.print(" ");       //空白部分                    }                }                System.out.println();                //換行            }        }    }}

 

java楊輝三角和空心菱形(二維數組篇)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.