Java 用星星打印男人的楼梯
我应该完成这个方法Java 用星星打印男人的楼梯,java,Java,我应该完成这个方法 public static void printStairs(int numMen) { } 哪张照片 o ****** /|\ * * / \ * * o ****** * /|\ * * / \ * * o ******
public static void printStairs(int numMen) {
}
哪张照片
o ******
/|\ * *
/ \ * *
o ****** *
/|\ * *
/ \ * *
o ****** *
/|\ * *
/ \ * *
*********************
我得到
public static void printStairs(int numMen) {
for (int i = 0; i < numMen; i++) {
String space = "";
String space2="";
for (int j = numMen - i; j > 1; j--) {
space += " ";
}
for (int j = 2; j <=numMen-i; j++) {
space2 += " ";
}
System.out.println(space + " o ******"+space2+"*");
System.out.println(space + " /|\\ * "+space2+"*");
System.out.println(space + " / \\ * "+space2+"*");
}
for(int i=0; i<=5*numMen+6; i++){
System.out.print("*");
}
}
而不是我想要的图像
我不明白为什么这不起作用,因为我只是颠倒了楼梯左侧空间的代码,并将其连接到楼梯右侧
有人知道如何将垂直线合并到代码中并创建所需的图像吗?从概念上讲,在多个位置的正方形上循环应该更容易:
public static void printStairs(int numMen)
{
String[] manLines = {
" o *****",
"/|\ * ",
"/ \ * "
};
final int rows = manLines.length;
for (int y = 0; y < numMen; ++y) {
for (int yrow = 0; yrow < rows; ++yrow) { // rows per man
for (int x = 0; x < y; ++x) {
System.out.print(" ");
}
System.out.print(manLines[yrow]);
for (int x = y; x < numMen; ++x) {
System.out.print(" ");
}
System.out.println("*");
}
}
}
public static void打印楼梯(int numMen)
{
字符串[]多行={
“o****”,
"/|\ * ",
"/ \ * "
};
最终int行=manLines.length;
对于(int y=0;y
试试这个:
public static void printMan(int numMen) {
for (int i = 0; i < numMen; i++) {
String space ="" ,space2 ="";
for(int j = numMen-i; j>1; j--) {
space += " ";
}
for(int k = 0; k<i ; k++) {
space2 += " ";
}
System.out.println(space +" o *****" + space2 + "*");
System.out.println(space + "/|\\ * "+ space2 + "*");
System.out.println(space + "/ \\ * "+ space2 + "*");
}
for (int i = 0; i < (numMen *10)-((numMen-1) *3); i++) {
System.out.print("*");
}
}
}
publicstaticvoidprintman(int numMen){
对于(int i=0;i1;j--){
空格+=“”;
}
对于(int k=0;k,使用字符数组使它更容易
private static final char[][] figure = new char[][] { " o ******".toCharArray(),
" /|\\ *".toCharArray(),
" / \\ *".toCharArray()};
private static void printStairs(int numMen)
{
int stairs = numMen + 1;
int width = (stairs * 5) + 1;
for (int i = 0; i < numMen; i++)
{
for (char[] part : figure)
{
char[] line = newLine(width, ' ');
// start at least the 11 back from the end - back another 5 for each step
System.arraycopy(part, 0, line, width - 11 - i * 5, part.length);
System.out.println(line);
}
}
System.out.println(newLine(width, '*'));
}
private static char[] newLine(int width, char fill)
{
char[] line = new char[width];
Arrays.fill(line, fill);
line[width - 1] = '*';
return line;
}
private static final char[][]figure=new char[][]{“o*******”。toCharArray(),
“/|\\*”.toCharArray(),
“/\\*”.toCharArray()};
专用静态空心打印楼梯(整数)
{
内部楼梯=numMen+1;
内部宽度=(楼梯*5)+1;
对于(int i=0;i
我想你在第三个for循环中犯了一个打字错误。上面说我在给出这么多代码时犹豫不决,但最重要的提示是引入“正方形”之类的概念迭代。对于以下代码,在numMen上循环比在numMen*3上循环更容易。顺便说一句,这段代码并不是所有的工作。谢谢!到目前为止最有用!知道如何获得左侧的垂直线吗?我正在考虑为楼梯右侧的空间创建另一个for循环,但它不起作用^;哦,我明白了,请参见请不要在发布问题后编辑它。
private static final char[][] figure = new char[][] { " o ******".toCharArray(),
" /|\\ *".toCharArray(),
" / \\ *".toCharArray()};
private static void printStairs(int numMen)
{
int stairs = numMen + 1;
int width = (stairs * 5) + 1;
for (int i = 0; i < numMen; i++)
{
for (char[] part : figure)
{
char[] line = newLine(width, ' ');
// start at least the 11 back from the end - back another 5 for each step
System.arraycopy(part, 0, line, width - 11 - i * 5, part.length);
System.out.println(line);
}
}
System.out.println(newLine(width, '*'));
}
private static char[] newLine(int width, char fill)
{
char[] line = new char[width];
Arrays.fill(line, fill);
line[width - 1] = '*';
return line;
}