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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© cat73 黑马帝   /  2014-7-22 19:38  /  3670 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 cat73 于 2014-7-23 23:20 编辑

原题是 用Java实现输出以下图形:


当时正好开着Python的idle 就用Python写了一个 又翻译成了Java代码 实际输出效果如下

代码如下:
PaintLadder.java
  1. package test;
  2. /**
  3. #Python源代码:
  4. #By:Cat73 QQ 1901803382
  5. #2014年7月22日19:33:12

  6. #画图函数 width:台阶的宽度(至少为4) hight:台阶的高度(至少为4) count:台阶的数量(至少为3)
  7. def paint(width, hight, count):
  8.     for i in range(1, count):
  9.         other(width, hight, count, i)
  10.     #画出最后一行
  11.     printCount('*', (width - 1) * (count) + 1, True)

  12. #画出一级台阶
  13. def other(width, hight, count, num):
  14.     paintPerson(width, hight, count, num, 0)
  15.     printCount('*', width)
  16.     printCount(' ', (width - 1) * (num - 1) - 1)
  17.     if(num == 1):
  18.         print('')
  19.     else:
  20.         print('*')
  21.    
  22.     for i in range(hight - 2):
  23.         paintPerson(width, hight, count, num, i + 1)
  24.         printCount('*', 1)
  25.         printCount(' ', (width - 1) * (num) - 1)
  26.         printCount('*', 1, True)

  27. #画小人 width/hight/count/num:外部传递 n:小人的第几行
  28. def paintPerson(width, hight, count, num, i):
  29.     tmp = int(width / 2) - 2
  30.     printCount(' ', (width - 1) * (count - num - 1) + tmp)

  31.     n = (hight - 1) - i
  32.     if(n == 3):
  33.         print(" o ", end="")
  34.     elif(n == 2):
  35.         print("/|\\", end="")
  36.     elif(n == 1):
  37.         print("/ \\", end="")
  38.     else:
  39.         print("   ", end="")

  40.     printCount(' ', width - 4 - tmp)
  41.    
  42. #打印一个字符一定次数 c:要打印的字符 i:要打印的次数 n:是否在打印完成后换行
  43. def printCount(c, i, n = False):
  44.     for i in range(i):
  45.         print(c, end="")
  46.     if(n):
  47.         print('')

  48. #调用画图测试
  49. paint(4, 4, 3)
  50. paint(5, 4, 4)
  51. paint(8, 8, 5)

  52. */


  53. /**
  54. * 画小人与阶梯的类(翻译自Python代码) <!>多线程不同步
  55. * @author Cat73
  56. */
  57. public final class PaintLadder {
  58.         /**
  59.          * 要被返回的字符缓存
  60.          * */
  61.         private StringBuffer buffer = null;
  62.         /**
  63.          * 操作缓存的指针
  64.          */
  65.         private int pBuffer;

  66.         /**
  67.          * 画小人阶梯
  68.          * @param width 阶梯宽度
  69.          * @param hight 阶梯高度
  70.          * @param count 阶梯数量
  71.          * @return 画好的字符串
  72.          */
  73.         public String paint(int width, int hight, int count) {
  74.                 //初始化缓存
  75.                 int length = ((hight - 1) * (count - 1) + 1) * ((width - 1) * count + 2);
  76.                 buffer = new StringBuffer(length);
  77.                 pBuffer = 0;
  78.                
  79.                 //画出每一级阶梯
  80.                 for (int i = 1; i < count; i++) {
  81.                         other(width, hight, count, i);
  82.                 }
  83.                 //画出最后一行
  84.                 printCount('*', (width - 1) * (count) + 1, true);
  85.                 //将结果返回
  86.                 return buffer.toString();
  87.         }

  88.         /**
  89.          * 画出每一层阶梯
  90.          * @param width 直接从外部传递
  91.          * @param hight 直接从外部传递
  92.          * @param count 直接从外部传递
  93.          * @param num 正在画第几级阶梯
  94.          */
  95.         private void other(int width, int hight, int count, int num) {
  96.                 //画出一层阶梯的最上方一行
  97.                 paintPerson(width, hight, count, num, 0);
  98.                 printCount('*', width, false);
  99.                 printCount(' ', (width - 1) * (num - 1) - 1, false);
  100.                 if (num == 1) {
  101.                         printCount('\n', 1, false);
  102.                 } else {
  103.                         printCount('*', 1, true);
  104.                 }

  105.                 //画出一层阶梯的其他行
  106.                 for (int i = 0; i < hight - 2; i++) {
  107.                         paintPerson(width, hight, count, num, i + 1);
  108.                         printCount('*', 1, false);
  109.                         printCount(' ', (width - 1) * (num) - 1, false);
  110.                         printCount('*', 1, true);
  111.                 }
  112.         }

  113.         /**
  114.          * 画每一行小人以及小人之前的空格
  115.          * @param width 直接从外部传递
  116.          * @param hight 直接从外部传递
  117.          * @param count 直接从外部传递
  118.          * @param num 直接从外部传递
  119.          * @param i 该级阶梯的第几行
  120.          */
  121.         private void paintPerson(int width, int hight, int count, int num, int i){
  122.                 //tmp是为了让小人居中用的 画出小人前面的空格
  123.                 int tmp = (int)(width / 2) - 2;
  124.                 printCount(' ', (width - 1) * (count - num - 1) + tmp, false);
  125.                
  126.                 //画出小人的身体
  127.                 int n = (hight - 1) - i;
  128.                 switch(n){
  129.                 case 3:
  130.                         outBuffer(" o ");
  131.                         break;
  132.                 case 2:
  133.                         outBuffer("/|\\");
  134.                         break;
  135.                 case 1:
  136.                         outBuffer("/ \\");
  137.                         break;
  138.                 default:
  139.                         //不需要画小人的用空格代替
  140.                         outBuffer("   ");
  141.                 }
  142.                
  143.         //画出小人后的空格
  144.     printCount(' ', width - 4 - tmp, false);
  145.         }

  146.         /**
  147.          * 打印一个字符N次(这里没打印 调用了加入缓存)
  148.          * @param c 要打印的字符
  149.          * @param count 要打印的次数
  150.          * @param enter 打印完后是否要加换行
  151.          */
  152.         private void printCount(char c, int count, boolean enter) {
  153.                 for (int i = 0; i < count; i++) {
  154.                         outBuffer(c);
  155.                 }
  156.                 if (enter) {
  157.                         outBuffer('\n');
  158.                 }
  159.         }

  160.         /**
  161.          * 加入字符串到缓存
  162.          * @param s 要加入缓存的字符串
  163.          */
  164.         private void outBuffer(String s) {
  165.                 buffer.insert(pBuffer, s);
  166.                 pBuffer += s.length();
  167.         }

  168.         /**
  169.          * 加入字符到缓存
  170.          * @param c 要加入缓存的字符
  171.          */
  172.         private void outBuffer(char c) {
  173.                 buffer.insert(pBuffer, c);
  174.                 pBuffer++;
  175.         }
  176. }
复制代码

可以用以下代码进行测试:
  1.                 PaintLadder p = new PaintLadder();
  2.                 String s;

  3.                 s = p.paint(4, 4, 3);
  4.                 System.out.println(s);
  5.                
  6.                 s = p.paint(5, 4, 4);
  7.                 System.out.println(s);
  8.                
  9.                 s = p.paint(8, 8, 5);
  10.                 System.out.println(s);
复制代码
相关链接:http://bbs.itheima.com/thread-131707-1-1.html=============后续=============
不知谁能做出连以下格式都支持的程序捏?(初步思路为重写整个程序改成直接替换字符串的形式来画图形 这样逻辑也能简单不少)
  1.          o  
  2.      o  /|\
  3. o  /|\ / \ *****
  4. /|\ / \ *****   *
  5. / \ *****       *
  6. *****************
复制代码


===========后续===========
上图中的问题已解决
http://bbs.itheima.com/thread-132012-1-1.html

1 个回复

正序浏览
楼主真乃高手啊~
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马