使用双重循环,输出数字金字塔
生活随笔
收集整理的這篇文章主要介紹了
使用双重循环,输出数字金字塔
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼:
package net.text0702;import java.util.Scanner;/*** @author Mr.Wang* 根據輸入數字,輸出數字金字塔:輸入幾就是幾行,每行輸出當前行的數字*/ public class dome1 {static Scanner input = new Scanner(System.in);public static void main(String[] args) {System.out.print("請輸入您需要的幾行數字:");int line = input.nextInt(); // 循環行數for(int i = 0;i < line;i++) { // 每行的空格數規律為輸入行數-1for(int j = 0;j<line-i-1;j++) {System.out.print(" ");} // 每行輸出的個數為等差數列,規律為2*當前行-1 // 輸出內容為當前行for(int z = 0;z<2*i+1;z++) {System.out.print(i+1);}System.out.println();}} }?
運行結果如下:
?
轉載于:https://www.cnblogs.com/Dean-0/p/11119486.html
總結
以上是生活随笔為你收集整理的使用双重循环,输出数字金字塔的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Shell脚本之一 Shell脚本简介
- 下一篇: 使用keil建立标准STM32工程模版(