用嵌套的for循環編寫程序,要求通過這個嵌套的循環在屏幕上打印下列圖案:
2 回答

米琪卡哇伊
TA貢獻1998條經驗 獲得超6個贊
圖片上的圖形輸出代碼:
public class demo{ public static void main(String[] args){ for ( int i= 1 ;i<= 9 ;i++){ for ( int z=( 9 -i)* 2 ;z>= 1 ;z--){ System.out.print( " " ); } for ( int x= 1 ;x<=i;x++){ System.out.print(x+ " " ); } for ( int y=i- 1 ;y>= 1 ;y--){ System.out.print(y+ " " ); } System.out.println(); } } } |
顯示一個整數的所有素數因子:
public class demo { public static void main(String[] args) throws Exception { Scanner cin = new Scanner(System.in); System.out.print( "請輸入一個正整數:" ); int n = cin.nextInt(); System.out.print(n + "=" ); int count = 0 ; for ( int i = 2 ; i <= n; i++) { if (n % i == 0 ) { if (count == 0 ) System.out.print(i); else System.out.print( "*" + i); count++; n = n / i; i--; } } } } |

寶慕林4294392
TA貢獻2021條經驗 獲得超8個贊
添加回答
舉報
0/150
提交
取消