A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 谢铭 中级黑马   /  2013-8-8 00:39  /  1173 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 谢铭 于 2013-8-10 00:14 编辑

今天老师安排了下面这样一道题目,要求打印两个菱形,写是写出来了,用的是循环和if语句,但是代码太长了,代码我附在了图下面,可以进行优化吗?
怎么优化较快捷,又能实现相关功能。  
图形如下所示:  
                      *       *
                    ***     ***
                 *****   *****
               ******* *******
             *****************
               ******* *******
                  *****   *****
                   ***     ***
                      *       *
注:图可能有有点问题,不是很整齐。
代码如下:
  1. public class Demo{
  2. public static void main(String [] args){
  3.    for(int a=1; a<=5; a++){ //打印上半部两个三角形
  4.     for(int b=1; b<=5-a; b++){
  5.      System.out.print(" ");
  6.     }
  7.     for(int c=1; c<=a; c++){  
  8.     System.out.print("*");
  9.     }
  10.     for(int d=1; d<a; d++){
  11.      System.out.print("*");
  12.     }
  13.     for(int b=1; b<=5-a; b++){
  14.      System.out.print(" ");
  15.     }
  16.     for(int b=1; b<=4-a; b++){
  17.      System.out.print(" ");
  18.     }
  19.     for(int c=1; c<=a; c++){
  20.     System.out.print("*");
  21.     }
  22.     if(a==5){
  23.      System.out.print("***");
  24.      }else{
  25.       for(int d=1; d<a; d++){
  26.       System.out.print("*");
  27.     }
  28.     }
  29.     System.out.println();   
  30.    }
  31.    for(int e=1; e<=4; e++){  //打印下半部两个三角形
  32.     for(int f=1; f<=e; f++){
  33.      System.out.print(" ");
  34.     }
  35.     for(int g=1; g<=5-e; g++){  
  36.      System.out.print("*");
  37.     }
  38.     for(int h=1; h<5-e; h++){  
  39.      System.out.print("*");
  40.     }
  41.     for(int f=1; f<=e; f++){
  42.      System.out.print(" ");
  43.     }
  44.     for(int f=1; f<e; f++){
  45.      System.out.print(" ");
  46.     }
  47.     for(int g=1; g<=5-e; g++){
  48.      System.out.print("*");
  49.     }
  50.     for(int h=1; h<5-e; h++){
  51.      System.out.print("*");
  52.     }
  53.     System.out.println();
  54. }
  55. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

2 个回复

倒序浏览
那么多循环!!重复的太多了!
上部分按4块来打印,下部分也按4快来打印,就能少写几个循环了,当然我这不算最优!
  1. public class Demo{
  2.         public static void main(String [] args){
  3.                 for(int a=0; a<5; a++){ //打印上半部两个三角形
  4.                         for(int b=1; b<5-a; b++){
  5.                                 System.out.print(" ");
  6.                         }
  7.                         for(int c=0; c<a*2+1; c++){  
  8.                                 System.out.print("*");
  9.                         }
  10.                         for(int b=1; b<5-a; b++){
  11.                                 System.out.print("  ");
  12.                         }
  13.                         for(int c=0; c<a*2+1; c++){
  14.                                 System.out.print("*");
  15.                         }
  16.                         System.out.println();   
  17.                 }
  18.                 for(int a=0; a<4; a++){ //打印上半部两个三角形
  19.                         for(int b=0; b<a+1; b++){
  20.                                 System.out.print(" ");
  21.                         }
  22.                         for(int c=0; c<6-a*2+1; c++){  
  23.                                 System.out.print("*");
  24.                         }
  25.                         for(int b=0; b<a+1; b++){
  26.                                 System.out.print("  ");
  27.                         }
  28.                         for(int c=0; c<6-a*2+1; c++){  
  29.                                 System.out.print("*");
  30.                         }
  31.                         System.out.println();   
  32.            }
  33.         }
  34. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杨兴庭 + 1

查看全部评分

回复 使用道具 举报 1 0
Jiewin 发表于 2013-8-8 07:44
那么多循环!!重复的太多了!
上部分按4块来打印,下部分也按4快来打印,就能少写几个循环了,当然我这不算 ...

嗯,刚学的,对于代码不太了解,我好好看下你的代码,我的代码是上半部分成了块,比较多.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马