您的位置:首页 > 编程语言 > Java开发

(Java)嵌套循环按要求输出数字序列

2013-11-29 16:37 295 查看
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();
}
}
}

         


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐