服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java利用for循环打印菱形的实例教程

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

2021-08-30 11:02才哈哈 Java教程

这篇文章主要给大家介绍了关于Java利用for循环打印菱形的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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      ######*######

代码示例:

  1. public class Demo {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. //为了输出效果在输出时将思路分析中的"#"替换为" "空格输出
  6.  
  7. for (int i = 1; i <= 11; i++) {
  8.  
  9. if (i <= 6) {
  10. //当i <=6时打印上三角形
  11. //模块 1
  12. for (int j = 6; j >= i; j--) {
  13. System.out.print(" ");
  14. }
  15. //模块 2
  16. for (int j = 0; j < i; j++) {
  17. System.out.print("*");
  18. }
  19. //模块 3[同一排中模块3的"*"个数总比模块2少1个,所以这里j <i-1作为循环条件即可]
  20. for (int j = 0; j < i - 1; j++) {
  21. System.out.print("*");
  22. }
  23. //模块 4
  24. for (int j = 6; j >= i; j--) {
  25. System.out.print(" ");
  26. }
  27. //换行
  28. System.out.println();
  29. } else {
  30. //当i >6时打印下三角形
  31. //模块 5
  32. for (int j = 7; j <= i + 1; j++) {
  33. System.out.print(" ");
  34. }
  35. //模块 6
  36. for (int j = 11; j >= i; j--) {
  37. System.out.print("*");
  38. }
  39. //模块 7
  40. for (int j = 11; j >= i + 1; j--) {
  41. System.out.print("*");
  42. }
  43. //模块 8
  44. for (int j = 7; j <= i + 1; j++) {
  45. System.out.print(" ");
  46. }
  47.  
  48. //换行
  49. System.out.println();
  50. }
  51. }
  52. }
  53. }

运行结果:

F:\software\Java\jdk1.8.0_121\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=50753:D:\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath F:\software\Java\jdk1.8.0_121\jre\lib\charsets.jar;F:\software\Java\jdk1.8.0_121\jre\lib\deploy.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;F:\software\Java\jdk1.8.0_121\jre\lib\javaws.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jce.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jfr.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jsse.jar;F:\software\Java\jdk1.8.0_121\jre\lib\management-agent.jar;F:\software\Java\jdk1.8.0_121\jre\lib\plugin.jar;F:\software\Java\jdk1.8.0_121\jre\lib\resources.jar;F:\software\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\ideastudyspace\spring_study\spring-study\spring-study-aop\target\classes;E:\software\MavenRepository\org\aspectj\aspectjweaver\1.9.4\aspectjweaver-1.9.4.jar;E:\software\MavenRepository\org\springframework\spring-webmvc\5.2.0.RELEASE\spring-webmvc-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-aop\5.2.0.RELEASE\spring-aop-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-beans\5.2.0.RELEASE\spring-beans-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-context\5.2.0.RELEASE\spring-context-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-core\5.2.0.RELEASE\spring-core-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-jcl\5.2.0.RELEASE\spring-jcl-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-expression\5.2.0.RELEASE\spring-expression-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-web\5.2.0.RELEASE\spring-web-5.2.0.RELEASE.jar;E:\software\MavenRepository\com\alibaba\fastjson\1.2.47\fastjson-1.2.47.jar com.shiguang.test.Demo
      *     
     ***    
    *****   
   *******  
  ********* 
 ***********
  ********* 
   *******  
    *****   
     ***    
      *

Process finished with exit code 0

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

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

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

代码示例:

  1. public class Demo2 {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. for (int i = 1; i <= 11; i++) {
  6.  
  7. if (i <= 6) {
  8. //当i <=6时打印上三角形
  9. //模块 1
  10. for (int j = 6; j >= i; j--) {
  11. System.out.print("*");
  12. }
  13. //模块 2
  14. for (int j = 0; j < i; j++) {
  15. System.out.print(" ");
  16. }
  17. //模块 3[同一排中模块3的"*"个数总比模块2少1个,所以这里j <i-1作为循环条件即可]
  18. for (int j = 0; j < i - 1; j++) {
  19. System.out.print(" ");
  20. }
  21. //模块 4
  22. for (int j = 6; j >= i; j--) {
  23. System.out.print("*");
  24. }
  25. //换行
  26. System.out.println();
  27. } else {
  28. //当i >6时打印下三角形
  29. //模块 5
  30. for (int j = 7; j <= i + 1; j++) {
  31. System.out.print("*");
  32. }
  33. //模块 6
  34. for (int j = 11; j >= i; j--) {
  35. System.out.print(" ");
  36. }
  37. //模块 7
  38. for (int j = 11; j >= i + 1; j--) {
  39. System.out.print(" ");
  40. }
  41. //模块 8
  42. for (int j = 7; j <= i + 1; j++) {
  43. System.out.print("*");
  44. }
  45.  
  46. //换行
  47. System.out.println();
  48. }
  49. }
  50. }
  51. }

运行结果:

F:\software\Java\jdk1.8.0_121\bin\java.exe "-javaagent:D:\IntelliJ IDEA 2019.1.4\lib\idea_rt.jar=50681:D:\IntelliJ IDEA 2019.1.4\bin" -Dfile.encoding=UTF-8 -classpath F:\software\Java\jdk1.8.0_121\jre\lib\charsets.jar;F:\software\Java\jdk1.8.0_121\jre\lib\deploy.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;F:\software\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;F:\software\Java\jdk1.8.0_121\jre\lib\javaws.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jce.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jfr.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;F:\software\Java\jdk1.8.0_121\jre\lib\jsse.jar;F:\software\Java\jdk1.8.0_121\jre\lib\management-agent.jar;F:\software\Java\jdk1.8.0_121\jre\lib\plugin.jar;F:\software\Java\jdk1.8.0_121\jre\lib\resources.jar;F:\software\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\ideastudyspace\spring_study\spring-study\spring-study-aop\target\classes;E:\software\MavenRepository\org\aspectj\aspectjweaver\1.9.4\aspectjweaver-1.9.4.jar;E:\software\MavenRepository\org\springframework\spring-webmvc\5.2.0.RELEASE\spring-webmvc-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-aop\5.2.0.RELEASE\spring-aop-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-beans\5.2.0.RELEASE\spring-beans-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-context\5.2.0.RELEASE\spring-context-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-core\5.2.0.RELEASE\spring-core-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-jcl\5.2.0.RELEASE\spring-jcl-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-expression\5.2.0.RELEASE\spring-expression-5.2.0.RELEASE.jar;E:\software\MavenRepository\org\springframework\spring-web\5.2.0.RELEASE\spring-web-5.2.0.RELEASE.jar;E:\software\MavenRepository\com\alibaba\fastjson\1.2.47\fastjson-1.2.47.jar com.shiguang.test.Demo
****** ******
*****   *****
****     ****
***       ***
**         **
*           *
**         **
***       ***
****     ****
*****   *****
****** ******

Process finished with exit code 0

总结

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

原文链接:https://blog.csdn.net/weixin_45333509/article/details/115253945

延伸 · 阅读

精彩推荐