日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java打印两个小人_[原创]Java画小人与阶梯问题的解答

發(fā)布時間:2025/3/12 java 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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画小人与阶梯问题的解答的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。