(Java)嵌套循環按要求輸出數字序列

1、顯示數字構成的金字塔,如下

                                             1
                                         2  1  2
                                     3  2  1  2  3
                                 4  3  2  1  2  3  4
                             5  4  3  2  1  2  3  4  5
                         6  5  4  3  2  1  2  3  4  5  6
                     7  6  5  4  3  2  1  2  3  4  5  6  7
                 8  7  6  5  4  3  2  1  2  3  4  5  6  7  8
             9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9
       10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10

實現代碼:

public class PrintPyramid {
	public static void main(String[] args){
		int numberofLines=10;
		for(int row=1;row<=numberofLines;row++){
			for(int column=1;column<=numberofLines-row;column++)
				System.out.print("   ");//3個領頭空格
			for(int num=row;num>=1;num--)
				System.out.print((num>=10)?" "+num:"  "+num);//大於10,數字前一個空格
			for(int num=2;num<=row;num++)
				System.out.print((num>=10)?" "+num:"  "+num);//小於10,數字前兩個空格
			System.out.println();
		}
	}
}


2、顯示數字構成的金字塔,如下

                             1
                       1    2   1
                  1   2    4   2   1
             1   2   4    8   4   2   1
       1    2   4   8  16   8   4   2   1
 1    2    4   8 16  32  16   8   4   2   1

實現代碼:

public class PrintPyramid2 {
	public static void main(String[] args){
		int numberofLines=6;
		for(int row=0;row<numberofLines;row++){
			for(int column=1;column<=numberofLines-row;column++)
				System.out.print("   ");
			for(int num=0;num<row;num++){
				System.out.print((Math.pow(2, num)>=10)?" "+(int)Math.pow(2, num):"  "+(int)Math.pow(2, num));
			}
			for(int num2=row;num2>=0;num2--)
				System.out.print((Math.pow(2, num2)>=10)?" "+(int)Math.pow(2, num2):"  "+(int)Math.pow(2, num2));
			System.out.println();
		}
	}
}

3、輸出左下三角,如下

 1
 1  2
 1  2  3
 1  2  3  4
 1  2  3  4  5
 1  2  3  4  5  6

實現代碼:

public class Print1 {
	public static void main(String[] args){
		int row=6;
		for(int i=1;i<=row;i++){
			for(int j=1;j<=i;j++){
				System.out.print(" "+j);
			}
			System.out.println();
		}
	}
}

4、輸出左上三角,如下

 1  2  3  4  5  6
 1  2  3  4  5
 1  2  3  4
 1  2  3
 1  2
 1
實現代碼:

public class Print2 {
	public static void main(String[] args){
		int row=6;
		for(int i=1;i<=row;i++){
			for(int j=1;j<=row+1-i;j++){
				System.out.print(" "+j);
			}
			System.out.println();
		}
	}
}


5、輸出右下三角,如下

                    1
                2  1
            3  2  1
        4  3  2  1
    5  4  3  2  1
6  5  4  3  2  1

實現代碼:

public class Print3 {
	public static void main(String[] args){
		int row=6;
		for(int i=1;i<=row;i++){
			for(int j=2*(row-i);j>0;j--)
				System.out.print(" ");
			for(int j=i;j>0;j--)
				System.out.print(j+" ");
			System.out.println();
		}
	}
}


6、輸出右上三角,如下

1  2  3  4  5  6
    1  2  3  4  5
        1  2  3  4
            1  2  3
                1  2
                    1

實現代碼:

public class Print4 {
	public static void main(String[] args){
		int row=6;
		for(int i=1;i<=row;i++){
			for(int j=1;j<=2*i;j++)
				System.out.print(" ");
			for(int j=1;j<=row+1-i;j++)
				System.out.print(j+" ");
			System.out.println();
		}
	}
}


         


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章