本帖最后由 cat73 于 2014-7-23 23:20 编辑
原题是 用Java实现输出以下图形:
当时正好开着Python的idle 就用Python写了一个 又翻译成了Java代码 实际输出效果如下
代码如下:
PaintLadder.java
- package test;
- /**
- #Python源代码:
- #By:Cat73 QQ 1901803382
- #2014年7月22日19:33:12
- #画图函数 width:台阶的宽度(至少为4) hight:台阶的高度(至少为4) count:台阶的数量(至少为3)
- def paint(width, hight, count):
- for i in range(1, count):
- other(width, hight, count, i)
- #画出最后一行
- printCount('*', (width - 1) * (count) + 1, True)
- #画出一级台阶
- def other(width, hight, count, num):
- paintPerson(width, hight, count, num, 0)
- printCount('*', width)
- printCount(' ', (width - 1) * (num - 1) - 1)
- if(num == 1):
- print('')
- else:
- print('*')
-
- for i in range(hight - 2):
- paintPerson(width, hight, count, num, i + 1)
- printCount('*', 1)
- printCount(' ', (width - 1) * (num) - 1)
- printCount('*', 1, True)
- #画小人 width/hight/count/num:外部传递 n:小人的第几行
- def paintPerson(width, hight, count, num, i):
- tmp = int(width / 2) - 2
- printCount(' ', (width - 1) * (count - num - 1) + tmp)
- n = (hight - 1) - i
- if(n == 3):
- print(" o ", end="")
- elif(n == 2):
- print("/|\\", end="")
- elif(n == 1):
- print("/ \\", end="")
- else:
- print(" ", end="")
- printCount(' ', width - 4 - tmp)
-
- #打印一个字符一定次数 c:要打印的字符 i:要打印的次数 n:是否在打印完成后换行
- def printCount(c, i, n = False):
- for i in range(i):
- print(c, end="")
- if(n):
- print('')
- #调用画图测试
- paint(4, 4, 3)
- paint(5, 4, 4)
- paint(8, 8, 5)
- */
- /**
- * 画小人与阶梯的类(翻译自Python代码) <!>多线程不同步
- * @author Cat73
- */
- public final class PaintLadder {
- /**
- * 要被返回的字符缓存
- * */
- private StringBuffer buffer = null;
- /**
- * 操作缓存的指针
- */
- private int pBuffer;
- /**
- * 画小人阶梯
- * @param width 阶梯宽度
- * @param hight 阶梯高度
- * @param count 阶梯数量
- * @return 画好的字符串
- */
- public String paint(int width, int hight, int count) {
- //初始化缓存
- int length = ((hight - 1) * (count - 1) + 1) * ((width - 1) * count + 2);
- buffer = new StringBuffer(length);
- pBuffer = 0;
-
- //画出每一级阶梯
- for (int i = 1; i < count; i++) {
- other(width, hight, count, i);
- }
- //画出最后一行
- printCount('*', (width - 1) * (count) + 1, true);
- //将结果返回
- return buffer.toString();
- }
- /**
- * 画出每一层阶梯
- * @param width 直接从外部传递
- * @param hight 直接从外部传递
- * @param count 直接从外部传递
- * @param num 正在画第几级阶梯
- */
- private void other(int width, int hight, int count, int num) {
- //画出一层阶梯的最上方一行
- paintPerson(width, hight, count, num, 0);
- printCount('*', width, false);
- printCount(' ', (width - 1) * (num - 1) - 1, false);
- if (num == 1) {
- printCount('\n', 1, false);
- } else {
- printCount('*', 1, true);
- }
- //画出一层阶梯的其他行
- for (int i = 0; i < hight - 2; i++) {
- paintPerson(width, hight, count, num, i + 1);
- printCount('*', 1, false);
- printCount(' ', (width - 1) * (num) - 1, false);
- printCount('*', 1, true);
- }
- }
- /**
- * 画每一行小人以及小人之前的空格
- * @param width 直接从外部传递
- * @param hight 直接从外部传递
- * @param count 直接从外部传递
- * @param num 直接从外部传递
- * @param i 该级阶梯的第几行
- */
- private void paintPerson(int width, int hight, int count, int num, int i){
- //tmp是为了让小人居中用的 画出小人前面的空格
- int tmp = (int)(width / 2) - 2;
- printCount(' ', (width - 1) * (count - num - 1) + tmp, false);
-
- //画出小人的身体
- int n = (hight - 1) - i;
- switch(n){
- case 3:
- outBuffer(" o ");
- break;
- case 2:
- outBuffer("/|\\");
- break;
- case 1:
- outBuffer("/ \\");
- break;
- default:
- //不需要画小人的用空格代替
- outBuffer(" ");
- }
-
- //画出小人后的空格
- printCount(' ', width - 4 - tmp, false);
- }
- /**
- * 打印一个字符N次(这里没打印 调用了加入缓存)
- * @param c 要打印的字符
- * @param count 要打印的次数
- * @param enter 打印完后是否要加换行
- */
- private void printCount(char c, int count, boolean enter) {
- for (int i = 0; i < count; i++) {
- outBuffer(c);
- }
- if (enter) {
- outBuffer('\n');
- }
- }
- /**
- * 加入字符串到缓存
- * @param s 要加入缓存的字符串
- */
- private void outBuffer(String s) {
- buffer.insert(pBuffer, s);
- pBuffer += s.length();
- }
- /**
- * 加入字符到缓存
- * @param c 要加入缓存的字符
- */
- private void outBuffer(char c) {
- buffer.insert(pBuffer, c);
- pBuffer++;
- }
- }
复制代码
可以用以下代码进行测试:
- PaintLadder p = new PaintLadder();
- String s;
- s = p.paint(4, 4, 3);
- System.out.println(s);
-
- s = p.paint(5, 4, 4);
- System.out.println(s);
-
- s = p.paint(8, 8, 5);
- System.out.println(s);
复制代码 相关链接:http://bbs.itheima.com/thread-131707-1-1.html=============后续=============
不知谁能做出连以下格式都支持的程序捏?(初步思路为重写整个程序改成直接替换字符串的形式来画图形 这样逻辑也能简单不少)
- o
- o /|\
- o /|\ / \ *****
- /|\ / \ ***** *
- / \ ***** *
- *****************
复制代码
===========后续===========
上图中的问题已解决
http://bbs.itheima.com/thread-132012-1-1.html
|
|