java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java for循环打印菱形

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

作者:才哈哈

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

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

总结

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

您可能感兴趣的文章:
阅读全文