close

問題描述:
撰寫一個程式,給你一個數字,請你參考範例輸入輸出的形式顯示圖形樣式。

輸入說明
輸入一個正整數,介於 1 ~ 9 。

輸出說明:請參考範例輸出。

範例:

題目來源:http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=1324

import java.util.Scanner;

public class C_ST14 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int n=scanner.nextInt();
            for(int i=0;i<n;i++){
                for(int j=0;j<n-i-1;j++){
                    System.out.print(" ");
                }
                int x=0;
                if(i%2==0){//奇數
                    x=1;
                }else{
                    x=i+1;
                }
                for(int j=0;j<i+1;j++){
                    System.out.print(x);
                    if(i%2==0){//奇數
                        x++;
                    }else{
                        x--;
                    }
                }
                System.out.println();
            }
        }
        scanner.close();
    }

}
 

arrow
arrow
    文章標籤
    Java ITSA
    全站熱搜

    阿雅 發表在 痞客邦 留言(0) 人氣()