java簡單列印金字塔(案例)

來源:互聯網
上載者:User

標籤:void   idt   can   輸入   text   java   開始   int   空格   

學習程式設計語言,列印簡單的金字塔是基礎的案例,在這裡就簡單的寫了個案例,希望能協助到各位

代碼

效果

class  kinTa{
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        for(int i = 1; i < 8; i++){   //控制行數
            for(int j = 0; j < 8-i;j++){   //每行要列印的空格數
                System.out.print(" ");
            }
            for(int k = 0; k < 2*i-1; k++){   //每行要列印的符號的個數(相對於前一行多兩個)
                System.out.print("*");
            }
            System.out.print("\n");   //換行列印
        }
        
    }
}

*

***

*****

*******

*********

***********

*************

***************

import java.util.Scanner; //匯入輸入函數庫

class  kinTaNum{
    public static void main(String[] args) {
        System.out.println("請輸入一個數字:");
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();  //控制台輸入一個整數
        for(int i = 1; i <= num; i++){   //行數為設定的那個數值
            for(int j = 0; j <= num-i;j++){   //空格站位
                System.out.print(" ");
            }
            for(int k = 1; k < i; k++){   //列印從1開始自增到設定的數
                System.out.print(k);
            }
            for(int m = i; m > 0 ; m--){   //列印從設定的數自減到1
                System.out.print(m);
            }
            System.out.print("\n");  //換行列印
        }
        
    }
}

(若輸入6)

1

121

12321

1234321

123454321

12345654321

陌陌說:這裡需要注意的是使用哪個列印方法
    1、println() 換行列印,列印一次就換一行
    2、print()   普通列印,即是不換行

   【java.util.Scanner;  是輸入函數庫】

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.