您的位置:首页技术文章
文章详情页

Java利用for循环打印菱形的实例教程

【字号: 日期:2022-08-15 09:55:21浏览:2作者:猪猪
Java for循环打印菱形

Java代码输出菱形的方法和思路有很多,在此分享一个稍带模块化拆分思想的解决方案,将需要输出的菱形拆分成8个模块(如下图),每个模块独立实现输出。

优点:8个模块之间耦合性降低,灵活性增强。也就是说我们可以独立的控制这8个模块中任意一个模块的输出内容来灵活应对业务逻辑的变更。

Java利用for循环打印菱形的实例教程

按照上图标好的顺序依次实现这8个模块的代码,当1458模块输出为空格时,其中2367模块的组合,就是一个菱形。

思路分析:

1 ######*###### 第一排 模块1[6个'#']+模块2[1个'*']+模块3[0个'*']+模块4[6个'#']2 #####***#####3 ####*****####4 ###*******###5 ##*********##6 #***********# --------------------------------------------------------7 ##*********## 第七排 模块5[2个'#']+模块6[5个'*']+模块7[4个'*']+模块8[2个'#']8 ###*******###9 ####*****#### 10 #####***##### 11 ######*######

代码示例:

public class Demo { public static void main(String[] args) { //为了输出效果在输出时将思路分析中的'#'替换为' '空格输出 for (int i = 1; i <= 11; i++) { if (i <= 6) { //当i <=6时打印上三角形 //模块 1 for (int j = 6; j >= i; j--) { System.out.print(' '); } //模块 2 for (int j = 0; j < i; j++) { System.out.print('*'); } //模块 3[同一排中模块3的'*'个数总比模块2少1个,所以这里j <i-1作为循环条件即可] for (int j = 0; j < i - 1; j++) { System.out.print('*'); } //模块 4 for (int j = 6; j >= i; j--) { System.out.print(' '); } //换行 System.out.println(); } else { //当i >6时打印下三角形 //模块 5 for (int j = 7; j <= i + 1; j++) { System.out.print(' '); } //模块 6 for (int j = 11; j >= i; j--) { System.out.print('*'); } //模块 7 for (int j = 11; j >= i + 1; j--) { System.out.print('*'); } //模块 8 for (int j = 7; j <= i + 1; j++) { System.out.print(' '); } //换行 System.out.println(); } } }}

运行结果:

F:softwareJavajdk1.8.0_121binjava.exe '-javaagent:D:IntelliJ IDEA 2019.1.4libidea_rt.jar=50753:D:IntelliJ IDEA 2019.1.4bin' -Dfile.encoding=UTF-8 -classpath F:softwareJavajdk1.8.0_121jrelibcharsets.jar;F:softwareJavajdk1.8.0_121jrelibdeploy.jar;F:softwareJavajdk1.8.0_121jrelibextaccess-bridge-64.jar;F:softwareJavajdk1.8.0_121jrelibextcldrdata.jar;F:softwareJavajdk1.8.0_121jrelibextdnsns.jar;F:softwareJavajdk1.8.0_121jrelibextjaccess.jar;F:softwareJavajdk1.8.0_121jrelibextjfxrt.jar;F:softwareJavajdk1.8.0_121jrelibextlocaledata.jar;F:softwareJavajdk1.8.0_121jrelibextnashorn.jar;F:softwareJavajdk1.8.0_121jrelibextsunec.jar;F:softwareJavajdk1.8.0_121jrelibextsunjce_provider.jar;F:softwareJavajdk1.8.0_121jrelibextsunmscapi.jar;F:softwareJavajdk1.8.0_121jrelibextsunpkcs11.jar;F:softwareJavajdk1.8.0_121jrelibextzipfs.jar;F:softwareJavajdk1.8.0_121jrelibjavaws.jar;F:softwareJavajdk1.8.0_121jrelibjce.jar;F:softwareJavajdk1.8.0_121jrelibjfr.jar;F:softwareJavajdk1.8.0_121jrelibjfxswt.jar;F:softwareJavajdk1.8.0_121jrelibjsse.jar;F:softwareJavajdk1.8.0_121jrelibmanagement-agent.jar;F:softwareJavajdk1.8.0_121jrelibplugin.jar;F:softwareJavajdk1.8.0_121jrelibresources.jar;F:softwareJavajdk1.8.0_121jrelibrt.jar;D:ideastudyspacespring_studyspring-studyspring-study-aoptargetclasses;E:softwareMavenRepositoryorgaspectjaspectjweaver1.9.4aspectjweaver-1.9.4.jar;E:softwareMavenRepositoryorgspringframeworkspring-webmvc5.2.0.RELEASEspring-webmvc-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-aop5.2.0.RELEASEspring-aop-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-beans5.2.0.RELEASEspring-beans-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-context5.2.0.RELEASEspring-context-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-core5.2.0.RELEASEspring-core-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-jcl5.2.0.RELEASEspring-jcl-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-expression5.2.0.RELEASEspring-expression-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-web5.2.0.RELEASEspring-web-5.2.0.RELEASE.jar;E:softwareMavenRepositorycomalibabafastjson1.2.47fastjson-1.2.47.jar com.shiguang.test.Demo * *** ***** ******* ********* *********** ********* ******* ***** *** *

Process finished with exit code 0

业务变更: 打印一个空心菱形

此时只需要将1458模块输出为'*',2367模块输出为空格即可得到一个空心菱形。

Java利用for循环打印菱形的实例教程

代码示例:

public class Demo2 { public static void main(String[] args) { for (int i = 1; i <= 11; i++) { if (i <= 6) { //当i <=6时打印上三角形 //模块 1 for (int j = 6; j >= i; j--) { System.out.print('*'); } //模块 2 for (int j = 0; j < i; j++) { System.out.print(' '); } //模块 3[同一排中模块3的'*'个数总比模块2少1个,所以这里j <i-1作为循环条件即可] for (int j = 0; j < i - 1; j++) { System.out.print(' '); } //模块 4 for (int j = 6; j >= i; j--) { System.out.print('*'); } //换行 System.out.println(); } else { //当i >6时打印下三角形 //模块 5 for (int j = 7; j <= i + 1; j++) { System.out.print('*'); } //模块 6 for (int j = 11; j >= i; j--) { System.out.print(' '); } //模块 7 for (int j = 11; j >= i + 1; j--) { System.out.print(' '); } //模块 8 for (int j = 7; j <= i + 1; j++) { System.out.print('*'); } //换行 System.out.println(); } } }}

运行结果:

F:softwareJavajdk1.8.0_121binjava.exe '-javaagent:D:IntelliJ IDEA 2019.1.4libidea_rt.jar=50681:D:IntelliJ IDEA 2019.1.4bin' -Dfile.encoding=UTF-8 -classpath F:softwareJavajdk1.8.0_121jrelibcharsets.jar;F:softwareJavajdk1.8.0_121jrelibdeploy.jar;F:softwareJavajdk1.8.0_121jrelibextaccess-bridge-64.jar;F:softwareJavajdk1.8.0_121jrelibextcldrdata.jar;F:softwareJavajdk1.8.0_121jrelibextdnsns.jar;F:softwareJavajdk1.8.0_121jrelibextjaccess.jar;F:softwareJavajdk1.8.0_121jrelibextjfxrt.jar;F:softwareJavajdk1.8.0_121jrelibextlocaledata.jar;F:softwareJavajdk1.8.0_121jrelibextnashorn.jar;F:softwareJavajdk1.8.0_121jrelibextsunec.jar;F:softwareJavajdk1.8.0_121jrelibextsunjce_provider.jar;F:softwareJavajdk1.8.0_121jrelibextsunmscapi.jar;F:softwareJavajdk1.8.0_121jrelibextsunpkcs11.jar;F:softwareJavajdk1.8.0_121jrelibextzipfs.jar;F:softwareJavajdk1.8.0_121jrelibjavaws.jar;F:softwareJavajdk1.8.0_121jrelibjce.jar;F:softwareJavajdk1.8.0_121jrelibjfr.jar;F:softwareJavajdk1.8.0_121jrelibjfxswt.jar;F:softwareJavajdk1.8.0_121jrelibjsse.jar;F:softwareJavajdk1.8.0_121jrelibmanagement-agent.jar;F:softwareJavajdk1.8.0_121jrelibplugin.jar;F:softwareJavajdk1.8.0_121jrelibresources.jar;F:softwareJavajdk1.8.0_121jrelibrt.jar;D:ideastudyspacespring_studyspring-studyspring-study-aoptargetclasses;E:softwareMavenRepositoryorgaspectjaspectjweaver1.9.4aspectjweaver-1.9.4.jar;E:softwareMavenRepositoryorgspringframeworkspring-webmvc5.2.0.RELEASEspring-webmvc-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-aop5.2.0.RELEASEspring-aop-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-beans5.2.0.RELEASEspring-beans-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-context5.2.0.RELEASEspring-context-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-core5.2.0.RELEASEspring-core-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-jcl5.2.0.RELEASEspring-jcl-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-expression5.2.0.RELEASEspring-expression-5.2.0.RELEASE.jar;E:softwareMavenRepositoryorgspringframeworkspring-web5.2.0.RELEASEspring-web-5.2.0.RELEASE.jar;E:softwareMavenRepositorycomalibabafastjson1.2.47fastjson-1.2.47.jar com.shiguang.test.Demo****** *********** ********* ******* ***** *** *** ***** ******* ********* *********** ******

Process finished with exit code 0

总结

到此这篇关于Java利用for循环打印菱形的文章就介绍到这了,更多相关Java for循环打印菱形内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Java
相关文章: