首页 > 编程知识 正文

用while循环打印菱形,用for循环打印菱形

时间:2023-05-05 11:07:33 阅读:34631 作者:2118

方法1 :

jm*i n

内层循环变量j每1个循环的增量次数*外层循环变量的第1个循环的循环次数

*1

**2

*****5

*******7

******9

*******7

*****5

**3

*1

分析:

从星星的第1行至第5行,各行各增加2,因此m为2;

从第五行到第九行,每行减少2; 所以m是-2;

从第一行到第五行,左边的空间减少一行。

从第五行到第九行,左边的空格各添加一行;

公共类star2{ publicstaticvoidmain (string [ ] args ) for ) intI=0; i 5; I ) for(intj=0; j -i 4; j ) (/1打印空间m=-1; System.out.print (' ); }for(intj=0; j 2 * i 1; j ) (/2打印星级m=2n=1system.out.print (' * '; } System.out.println (; //3换行}for(intI=0; i 4; I ) for(intj=0; j i 1; j ) (/1打印空间m=1; n=1 System.out.print (' ); }for(intj=0; j -2 * i 7; 打印j ()/2星m=-2; n=7system.out.print('* '; } System.out.println (; //3换行} }方法2 :

公共语音exe1((scannersc=new scanner ) system.in ); int rows=sc.nextInt (; for(introw=1; row=2*rows-1; row () for ) intcol=1; col=math.ABS(rows-row ); col({system.out.print (' ) ); }for(intcol=1; col=2* rows-1-2 * math.ABS (rows-row ); col ) (system.out.print ) ) (* ); } System.out.println (; }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。