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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 毋须繁华 中级黑马   /  2013-9-4 22:11  /  1177 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 毋须繁华 于 2013-9-4 22:14 编辑
  1. <p><p><p> 输入菱形高度和和列数、行数输出菱形。如:输入9,2,2 将输出如下图形
  2.        #        #
  3.       ###      ###
  4.      #####    #####
  5.     #######  #######
  6.    ##################
  7.     #######  #######
  8.      #####    #####
  9.       ###      ###
  10.        #        #   
  11.       ###      ###
  12.      #####    #####
  13.     #######  #######
  14.    ##################
  15.     #######  #######
  16.      #####    #####
  17.       ###      ###
  18.        #        #
  19. int gao=9;
  20.   for(int x=0;x<2;x++){
  21.    for(int i=0; i<(gao+1) / 2; i++) {  
  22.     //打印正序
  23.     for(int z=0;z<2;z++){
  24.         for(int j=0; j<=gao/2-i; j++) { //打印每行的空格
  25.          System.out.print(" ");
  26.         }
  27.         for(int k=1; k<(i+1)*2; k++) {  //打印每行的*
  28.          System.out.print('*');
  29.         }
  30.         for(int j=0; j<=gao/2-i; j++) { //打印每行的空格
  31.          System.out.print(" ");
  32.         }
  33.     }
  34.        System.out.println();
  35.     }
  36.      //打印倒序
  37.      for(int i=1; i<=gao/2; i++) {
  38.       for(int z=0;z<2;z++){
  39.        for(int j=0; j<=i; j++) {
  40.          System.out.print(" ");
  41.         }
  42.         for(int k=1; k<=gao-2*i; k++) {
  43.          System.out.print('*');
  44.         }
  45.         for(int j=0; j<=i; j++) {
  46.          System.out.print(" ");
  47.         }
  48.       }
  49.        System.out.println();     
  50.     }     
  51.    }
  52. </p><p> </p></p></p></p>
复制代码
求高手帮忙把代码优化一下。我写的这种貌似太繁琐了……

4 个回复

倒序浏览
题目的要求你理解错了,你是写出922的菱形了,不过题目要求是能输出任意菱形,而你只是做出了9,2,2类型,你是不是还没学到面向对象吧?从头到属只有一个主函数,也只有一个类,建议先把前10天的视频看完再来做这道题,那时就简单些
回复 使用道具 举报
路边小色狼 发表于 2013-9-4 22:46
题目的要求你理解错了,你是写出922的菱形了,不过题目要求是能输出任意菱形,而你只是做出了9,2,2类型, ...
  1. LingXing l = new LingXing();
  2.                 l.lingXing(11,2,2);
  3.        
  4. class LingXing{
  5.         void lingXing(int gao,int hang,int lie ){
  6.                 for(int x=0;x<hang;x++){
  7.                         for(int i=0; i<(gao+1) / 2; i++) {               
  8.                                 //打印正序
  9.                                 for(int z=0;z<lie;z++){
  10.                              for(int j=0; j<=gao/2-i; j++) { //打印每行的空格
  11.                               System.out.print(" ");
  12.                              }
  13.                              for(int k=1; k<(i+1)*2; k++) {  //打印每行的*
  14.                               System.out.print('*');
  15.                              }
  16.                              for(int j=0; j<=gao/2-i; j++) { //打印每行的空格
  17.                               System.out.print(" ");
  18.                              }
  19.                                 }
  20.                      System.out.println();
  21.                   }
  22.                    //打印倒序
  23.             for(int i=1; i<=gao/2; i++) {
  24.                     for(int z=0;z<lie;z++){
  25.                             for(int j=0; j<=i; j++) {
  26.                               System.out.print(" ");
  27.                              }
  28.                              for(int k=1; k<=gao-2*i; k++) {
  29.                                      System.out.print('*');
  30.                              }
  31.                              for(int j=0; j<=i; j++) {
  32.                               System.out.print(" ");
  33.                              }
  34.                     }
  35.                             System.out.println();                  
  36.                   }                          
  37.           }       
  38.         }
  39. }
复制代码
我想要个优化的- -
回复 使用道具 举报
我有个思路,但不知道可不可行,有空再写看看,先做出第一高度为x个菱形,(x肯定为奇数),你注意观察的话能发现,第一个菱形往右移x个坐标就能得右边那个,向下移x个坐标就得下面那个。明天写写看好了
回复 使用道具 举报
本帖最后由 张文豪 于 2013-9-6 08:18 编辑
  1. <p> //需求:输入菱形高度,列数,行数。输出图形
  2. class printLengx
  3. {
  4. public static void method_space(int x,int h)//输出空格的方法
  5. {
  6.   for(int y=h-x;y>0;y--)
  7.   {
  8.    System.out.print(" ");
  9.   }
  10. }
  11. public static void method_a(int x)//输出*的方法
  12. {
  13.   for(int z=0;z<2*x-1;z++)
  14.   {
  15.    System.out.print("*");
  16.   }
  17. }
  18. public static void method_1(int hang,int high)//输出一行菱形的方法
  19. {
  20.   int h=(high+1)/2;
  21.   for(int x=1;x<=high;x++)
  22.   {
  23.    for(int a=1;a<=hang;a++)
  24.    {
  25.     int temp = (x<=h)?x:(2*h-x);
  26.     method_space(temp,h);
  27.     method_a(temp);
  28.     method_space(temp,h);
  29.    }
  30.    System.out.println("");
  31.   }</p><p> }
  32. public static void methodLing(int hang,int lie,int high)//输出行高列的菱形方法
  33. {
  34.   for(int x = 0; x<lie;x++)
  35.    method_1(hang,high);
  36. }

  37. public static void main(String[] args)//高度是奇数,至于输入偶数,抛异常就不写了。
  38. {
  39.   methodLing(8,4,9);
  40. }
  41. }</p></p></p><p><img border="0" alt="" src="http://bbs.itheima.com/forum.php?mod=image&aid=25771&size=300x300&key=4b19b42d10780486&nocache=yes&type=fixnone" aid="attachimg_25771"></p>
复制代码

图片1.png (14.58 KB, 下载次数: 8)

原理

原理
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马