• 利用for迴圈寫出能產生以下結果之程式:
112123123412345
  • 程式碼:
import java.util.Scanner;public class P5310 {	public static void main(String[] args) {		Scanner scn = new Scanner(System.in);		System.out.println("請輸入任一正整數:");		int a = scn.nextInt();		scn.close();
		// 第一次執行i迴圈,i為1,進j迴圈,j起始值1		// j迴圈執行條件 j<=i		// j須小於等於i才執行		// j=i=1,輸出j值1
		// 第二次執行i迴圈,i值2,進j迴圈,j起始值1		// j=1, j=2 小於等於i值2,j迴圈執行2次,輸出j值1和2		// 使用print(),不使用println(),前者連續印出,後者斷行印出
		for (int i = 1; i <= a; i++) {			for (int j = 1; j <= i; j++) {				System.out.print(j);			}			System.out.println();		}	}}
輸出結果(輸入9):
請輸入任一正整數:9112123123412345123456123456712345678123456789

 

arrow
arrow

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