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循環打印菱形內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: