小言_互联网的博客

Java for循环打印菱形

250人阅读  评论(0)

Java for循环打印菱形

Java代码输出菱形的方法和思路有很多,在此分享一个稍带模块化拆分思想的解决方案,将需要输出的菱形拆分成8个模块(如下图),每个模块独立实现输出。
优点:8个模块之间耦合性降低,灵活性增强。也就是说我们可以独立的控制这8个模块中任意一个模块的输出内容来灵活应对业务逻辑的变更。

按照上图标好的顺序依次实现这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:\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模块输出为空格即可得到一个空心菱形。

代码示例:

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:\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

转载:https://blog.csdn.net/weixin_45333509/article/details/115253945
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场