1
1 2
1 2 4
1 2 4 8
...
1 2 4 8 ....
The method header is
public static void displayPattern(int n)
import java.util.Scanner;
public class C1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
//提示使用者輸入
System.out.println("請輸入層數:");
int linenumber = input.nextInt();
//呼叫 displayPattern
displayPattern(linenumber);
}
public static void displayPattern(int n)
{
int b = 1;
for(int i = 1; i <= n; i++)//控制換行
{
for(int a = 1; a <= i; a++)//控制每行印出個數
{
System.out.print(b + " ");
b = b * 2;
}
System.out.println();//換行
b = 1;//因為第一個是從1開始印,所以印完一行後要將b的值變回1
}
}
}
沒有留言:
張貼留言