Loading... <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-3601e5cf53aa55f63195e93be41d4c4e38" aria-expanded="true"><div class="accordion-toggle"><span style="">题目</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-3601e5cf53aa55f63195e93be41d4c4e38" class="collapse in collapse-content"><p></p> ``` ◆用循环打印出下列图形: ********** //10星 * * * * * * * * * ********** //10星 (提示:若用单层循环,则可以考虑for+switch结构实现) ``` <p></p></div></div></div> ## 代码 ``` public class home { public static void main(String[] args) { String s = "*"; String n = " "; for (int l = 0; l < 11; l++) { //循环打印11行 switch (l){ case 0: case 10: System.out.println("**********"); //第一行和最后一行输出十个* break; default: //除开第一行和最后一行执行这个方法 s = "*"; n = ""; for (int j = 0; j <= 10-l; j++) { //根据行数来输出多少个“ ”(空格) n = n +" "; if (j == 9-l){ n = n + s; //末尾加上* } } System.out.println(n); } } /* 第一版 System.out.println("**********"); for (int i = 0; i < 9; i++) { s = "*"; n = ""; for (int j = 0; j <= 9-i; j++) { n = n +" "; if (j == 8-i){ n = n + s; } } System.out.println(n); } System.out.println("**********"); */ } } ``` 最后修改:2021 年 09 月 23 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 0 如果觉得我的文章对你有用,请随意赞赏