java打印两个小人_[原创]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++;
}
}
總結
以上是生活随笔為你收集整理的java打印两个小人_[原创]Java画小人与阶梯问题的解答的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scanf函数使用遇到的问题
- 下一篇: leetcode题目整数颠倒