本文主要是介绍Java打印圣诞树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
经典java基础循环操作题,圣诞树的打印。先展示下效果吧!
* *** ****** * *** *** ***** ***** * * * *** *** ***
***** ***** *****
//第一步、先循环打印出的格式
*
* *
* * *for (int i = 1; i <= 3; i++) {for (int j = 1; j <= i; j++) {System.out.print("*");System.out.print(" ");}System.out.println();}
// 第二步、然后循环打印五遍'*'符号
*****
***** *****
***** ***** *****for (int i = 1; i <= 3; i++) {for (int j = 1; j <= i; j++) {for(int k=1;k<=5;k++){System.out.print("*");}System.out.print(" ");}System.out.println();}
//第三步、使用空格布局********** *****
***** ***** *****//1.6 3*2=>3*(3-1) 3*(3-i)
//2.3 3*1=>3*(3-2)
//3.0 3*0=>3*(3-3)for (int i = 1; i <= 3; i++) {for(int j=1;j<=3*(3-i);j++){System.out.print(" ");}for (int j = 1; j <= i; j++) {for(int k=1;k<=5;k++){System.out.print("*");}System.out.print(" ");}System.out.println();}
//第四步、循环次数******************** ***** ***** ***** ***** *****
***** ***** *****
***** ***** *****
***** ***** *****for (int i = 1; i <= 3; i++) {for (int m = 1; m <= 3; m++) {for (int j = 1; j <= 3 * (3 - i); j++){System.out.print(" ");}for (int j = 1; j <= i; j++) {for (int k = 1; k <= 5; k++){System.out.print("*");}System.out.print(" ");}System.out.println();}}
//第五步(最后)、设置星号和空格
//空格
1.2 3-m
2.1
3.0//星号
1.1 2*m-1
2.3
3.5for (int i = 1; i <= 3; i++) {for (int m = 1; m <= 3; m++) {for (int j = 1; j <= 3 * (3 - i); j++){System.out.print(" ");}for (int j = 1; j <= i; j++) {for (int k = 1; k <= 3-m; k++){System.out.print(" ");}for (int k = 1; k <= 2*m-1; k++){System.out.print("*");}for (int k = 1; k <= 3-m; k++){System.out.print(" ");}System.out.print(" ");}System.out.println();}}
总结:for循环的嵌套使用
这篇关于Java打印圣诞树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!