2014年12月26日 星期五

java程式設計題目---圖形繪製【使用2的n次方印三角形(使用method)】

Write a method to display a pattern as follows:
1
1  2
1  2  4
1  2  4  8
      ...
1  2  4  8   ....
The method header is
public static void displayPattern(int n)


  1. import java.util.Scanner;
  2.  
  3. public class C1
  4. {
  5. public static void main(String[] args)
  6. {
  7. Scanner input = new Scanner(System.in);
  8. //提示使用者輸入
  9. System.out.println("請輸入層數:");
  10. int linenumber = input.nextInt();
  11. //呼叫 displayPattern
  12. displayPattern(linenumber);
  13. }
  14.  
  15. public static void displayPattern(int n)
  16. {
  17. int b = 1;
  18. for(int i = 1; i <= n; i++)//控制換行
  19. {
  20. for(int a = 1; a <= i; a++)//控制每行印出個數
  21. {
  22. System.out.print(b + " ");
  23. b = b * 2;
  24. }
  25. System.out.println();//換行
  26. b = 1;//因為第一個是從1開始印,所以印完一行後要將b的值變回1
  27. }
  28. }
  29. }

沒有留言:

張貼留言

Go(Golang)程式語言 設定GCC

下載MSYS2:  https://www.msys2.org/ 安裝 程式開始執行後輸入: pacman -Syu y y 在安裝目錄底下找到msys2.exe,雙擊執行 輸入指令: pacman -Su pacman -S --needed base-devel mingw-...