java 我无法理解“用火柴人画楼梯”程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11607943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:46:16  来源:igfitidea点击:

I can't wrap my head around the "draw some stairs with stick-men" program

javafor-loop

提问by Monica

You've probably seen it before in a Java 1 class: it's a problem that asks you write a program that draws the following figure:

您可能之前在 Java 1 类中见过它:这是一个问题,要求您编写一个绘制下图的程序:

enter image description here

在此处输入图片说明

I have to use a constant. I am not allowed to use anything but for-loops, print, and println. No parameters, no arrays. I know how I could do it with parameters and arrays, lucky me. Any help is appreciated!

我必须使用一个常数。除了 for 循环、print、 和,我不允许使用任何东西println。没有参数,没有数组。我知道如何使用参数和数组来做到这一点,我很幸运。任何帮助表示赞赏!

Here is my incomplete code:

这是我不完整的代码:

public class Stairs {
    public static final int LENGTH=5;

    public static void main(String[] args) {
        printStairs();
    }

    public static void printStairs() {
        for (int allStairs=1; allStairs<=15; allStairs++) {
            for (int spaces=1; spaces<=(-5*allStairs+30); spaces++) {
                System.out.print(" ");
            }
            for (int stair = 1; stair <= 5; stair++) {
                System.out.println("  o  *******");

            }
        }
    }
}

采纳答案by Jon Newmuis

This sounds like a homework question, so I don't just want to give you the answer, but try breaking it down into steps. Think about the things that you know:

这听起来像是一个家庭作业问题,所以我不只是想给你答案,而是试着把它分解成几个步骤。想想你知道的事情:

1) Every stickman has this shape:

1)每个火柴人都有这个形状:

  o  ******
 /|\ *     
 / \ *     

2) You can print this out using the following code:

2)您可以使用以下代码打印出来:

System.out.println("  o  ******");
System.out.println(" /|\ *     ");
System.out.println(" / \ *     ");

3) You can print multiple by using a loop:

3)您可以使用循环打印多个:

for (int stair = 1; stair <= LENGTH; stair++) {
    System.out.println("  o  ******");
    System.out.println(" /|\ *     ");
    System.out.println(" / \ *     ");
}

Think about what kind of output this would give you, and what needs to be changed. Realize that each stickman needs to be indented a certain amount. Figure out how to indent them appropriately based on the value of stair.

想想这会给你什么样的输出,以及需要改变什么。意识到每个火柴人都需要缩进一定数量。弄清楚如何根据 的值适当地缩进它们stair

回答by nhahtdh

You can look at the figure and observe that each stickman has 3 lines, for each line:

您可以查看该图并观察到每个火柴人有 3 行,每行:

  • Spaces, if necessary, proportionate to the number of stickmen
  • Part of the stick man - you can hard code this
  • The flat surface of 6 *, or one *followed by 5 spaces
  • Spaces, if necessary, proportionate to the number of stickmen
  • A *
  • 如有必要,与火柴人数量成比例的空格
  • 棒子的一部分 - 你可以硬编码这个
  • 6 *, 或 1*后 5 格的平面
  • 如有必要,与火柴人数量成比例的空格
  • 一个 *

And at the end, the last line is *proportionate to the number of stickmen.

最后,最后一行*与火柴人的数量成正比。

回答by Adriaan Koster

Here is my final final attempt using formatting.

这是我使用格式的最后一次尝试。

public static void main(String[] args) {
    int steps = 5;
    for (int x = 0; x < steps; x++) {
        System.out.format(((steps == (x + 1)) ? "" : ("%"
                + ((steps - x - 1) * 5) + "s"))
                + "  o  ******"
                + ((x == 0) ? "" : ("%" + (x * 5) + "s"))
                + "*\n", " ", " ");
        System.out.format(((steps == (x + 1)) ? "" : ("%"
                + ((steps - x - 1) * 5) + "s"))
                + " /|\ *     "
                + ((x == 0) ? "" : ("%" + (x * 5) + "s"))
                + "*\n", " ", " ");
        System.out.format(((steps == (x + 1)) ? "" : ("%"
                + ((steps - x - 1) * 5) + "s"))
                + " / \ *     "
                + ((x == 0) ? "" : ("%" + (x * 5) + "s"))
                + "*\n", " ", " ");
    }
    for (int i = 0; i < (steps + 1) * 5 + 2; i++) {
        System.out.print("*");
    }
}

Output:

输出:

                      o  *******
                     /|\ *     *
                     / \ *     *
                 o  ******     *
                /|\ *          *
                / \ *          *
            o  ******          *
           /|\ *               *
           / \ *               *
       o  ******               *
      /|\ *                    *
      / \ *                    *
  o  ******                    *
 /|\ *                         *
 / \ *                         *
********************************

\o/

\o/

The approach below is also funny (depending on your humor preferences), but not a complete solution:

下面的方法也很有趣(取决于你的幽默偏好),但不是一个完整的解决方案:

    for (String s = "                           o  *****                          /|\ *                              / \ *    "; s
            .charAt(8) != '*'; s = s.substring(5, s.length() / 3) + "     "
            + s.substring(s.length() / 3 + 5, 2 * s.length() / 3) + "     "
            + s.substring(2 * s.length() / 3 + 5, s.length()) + "     ") {
        System.out.println(s.substring(0, s.length() / 3) + "*");
        System.out
                .println(s.substring(s.length() / 3, 2 * (s.length() / 3))
                        + "*");
        System.out.println(s.substring(2 * (s.length() / 3), s.length())
                + "*");
    }

Output:

输出:

                       o  ******
                      /|\ *    *
                      / \ *    *
                  o  *****     *
                 /|\ *         *
                 / \ *         *
             o  *****          *
            /|\ *              *
            / \ *              *
        o  *****               *
       /|\ *                   *
       / \ *                   *
   o  *****                    *
  /|\ *                        *
  / \ *                        *

回答by Nishant

There is a recursive block with diminishing white space. The recursion ends at LEFT_SPACE == 0The recursive block is

有一个递减空白的递归块。递归结束于LEFT_SPACE == 0递归块是

LEFT_SPACE o  ******RIGHT_SPACE*
LEFT_SPACE/|\ *     RIGHT_SPACE*
LEFT_SPACE/ \ *     RIGHT_SPACE*

回答by arikg

Here are some tips:

以下是一些提示:

  • You should think about it horizontally.
  • your main loop must be stairs amount related because you are bound by the number of stairs you have to draw.
  • It will probably be easiest to print each line separately, so understand all the information you need to draw each line.
  • 你应该横向考虑。
  • 您的主循环必须与楼梯数量相关,因为您受到必须绘制的楼梯数量的限制。
  • 单独打印每条线可能最容易,因此请了解绘制每条线所需的所有信息。

回答by Monica

Here is what I ultimately ended up with:

这是我最终得到的结果:

public class Stairs {
    public static final int LENGTH=5;

    // The 'main method' prints out all the stairs with the appropriate indentations.
    public static void main(String[] args) {
        // outer loop
        for (int allStairs=0; allStairs<=4; allStairs++) {
            // first nested loops print the heads and tops of steps
            for (int spaces=1; spaces<=(-5*allStairs+20); spaces++) {
                printSpace();
            }
            System.out.print("  o  ******");
            for (int backWall=0; backWall<allStairs*(LENGTH); backWall++) {
                printSpace();
            }
            printStar();
            // second nexted loops print the body and the backs of the stairs
            for (int spaces = 1; spaces <= (-5 * allStairs + 20); spaces++) {
                printSpace();
            }
            System.out.print(" /|\ *");
            for (int backWall=1; backWall<=LENGTH*(allStairs+1); backWall++) {
                printSpace();
            }
            printStar();
            // third nested loops print the legs and lower backs of stairs
            for (int spaces = 1; spaces <= (-5 * allStairs + 20); spaces++) {
                printSpace();
            }
            System.out.print(" / \ *");
            for (int backWall=1; backWall<=LENGTH*(allStairs+1); backWall++) {
                printSpace();
            }
            printStar();
        }
        // this loop prints the very bottom line of stars
        for (int lastLine=1; lastLine<=32; lastLine++) {
            System.out.print("*");
        }
        System.out.println();
    }

    // printSpace() prints out a space
    public static void printSpace() {
        System.out.print(" ");
    }

    // printStar() prints out an asterisk
    public static void printStar() {
        System.out.println("*");
    }
}

回答by lucyloo222

I did it slightly differently. The following script will work with any number of stairs. I've commented it so you should know exactly what I am doing.

我的做法略有不同。以下脚本适用于任意数量的楼梯。我已经评论过了,所以你应该确切地知道我在做什么。

public class stairman {

//establishing class constants
public static final int STAIR_NUM = 5;
public static final int WIDTH = STAIR_NUM * 5;

public static void main(String[] args) {

    //perform this loop for as many times as there are stairs
    for (int i=1; i<=STAIR_NUM; i++) { 

        //calculating number of spaces before the top line of the stair
        for (int x=1; x<=(WIDTH+(i*(-5))); x++) {
            System.out.print(" ");
        }

        //printing top line of the stair
        head();

        //calculating the number of spaces after the top line of the stair
        for(int y=1; y<=((i-1)*5); y++){ 
            System.out.print(" ");
        }
        System.out.println("*");

        //calculating the number of spaces before the 1st middle line of the stair
        for (int x=1; x<=(WIDTH+(i*(-5))); x++) {
            System.out.print(" ");
        }

        //print the first middle line of the stair
        middle1();

        //calculating the number of spaces after the 1st middle line of the stair           
        for (int y=1; y<=(i*5); y++){ 
            System.out.print(" ");
        }
        System.out.println("*");

        //calculating the number of spaces before the 2nd middle line of the stair          
        for (int x=1; x<=(WIDTH+(i*(-5))); x++) {
            System.out.print(" ");
        }

        //printing the 2nd middle line of the stair
        middle2();

      //calculating the number of spaces after the 2nd middle line of the stair
        for (int y=1; y<=(i*5); y++){ 
            System.out.print(" ");
        }
        System.out.println("*");
    }

    //calculating the number of stars needed for the bottom of the stairs
    for (int z=1; z<=(WIDTH+7); z++) {
        System.out.print("*");
    }
}

public static void head() {
    System.out.print("  o  ******");
}
public static void middle1() {
        System.out.print(" /|\ *");
}
public static void middle2() {
    System.out.print(" / \ *");
}   

}

}

回答by Andrew Engelhardt

I tested the above code and it wasn't working for me. So I had a go at it and used some of their ideas.

我测试了上面的代码,但它对我不起作用。所以我尝试了一下,并使用了他们的一些想法。

public class DrawStairs {   

public static final int HEIGHT= 5;

public static final int TOTALHEIGHT= HEIGHT*5;

public static void main(String[] args){
    //Main Outer Loop
    for(int i=1; i<=HEIGHT; i++){

        //Loop for the spaces before, then print the head
        for(int j=1; j<=TOTALHEIGHT+(i*(-5)); j++){ 
            System.out.print(" ");
        }
        printTop();

        //Loop for spaces after, then print asterisk
        for(int j=1; j<=(i-1)*5; j++){ 
            System.out.print(" ");
        }
        System.out.println("*");

        //Loop for the spaces before, then print the body
        for(int j=1; j<=TOTALHEIGHT+(i*(-5)); j++){ 
            System.out.print(" ");
        }
        printMiddle();

        //Loop for spaces after, then print asterisk
        for(int j=1; j<=(i-1)*5; j++){ 
            System.out.print(" ");
        }
        System.out.println("*");

        //Loop for spaces before, then print the legs
        for(int j=1; j<=TOTALHEIGHT+(i*(-5)); j++){ 
            System.out.print(" ");
        }
        printBottom();

        //Loop for spaces after, then print asterisk
        for(int j=1; j<=(i-1)*5; j++){ 
            System.out.print(" ");
        }
        System.out.println("*");            
    }

    // for loop for printing the floor of asterisks
    for(int i=1; i<= TOTALHEIGHT+7; i++){
        System.out.print("*");
    }
}   

public static void printTop() {
    System.out.print("  o  ******");
}

public static void printMiddle() {
    System.out.print(" /|\ *     ");
}

public static void printBottom() {
    System.out.print(" / \ *     ");
}
}