我无法绕着“用棍子男人画楼梯”程序

时间:2012-07-23 07:13:25

标签: java for-loop

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

enter image description here

我必须使用常数。除了for循环printprintln之外,我不允许使用任何其他内容。没有参数,没有数组。我知道如何使用参数和数组来完成它,幸运的是我。任何帮助表示赞赏!

这是我不完整的代码:

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  *******");

            }
        }
    }
}

8 个答案:

答案 0 :(得分:26)

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

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

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

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

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

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

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

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

答案 1 :(得分:4)

你可以看一下这个图,观察每个火柴人每行有3行:

  • 如有必要,空格与坚持者的数量成比例
  • 棒人的一部分 - 你可以硬编码
  • 6 *的平坦表面,或一个*后跟5个空格
  • 如有必要,空格与坚持者的数量成比例
  • A *

最后,最后一行是*与坚持者的数量成比例。

答案 2 :(得分:3)

以下是我使用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("*");
    }
}

输出:

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

\ O /

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

    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())
                + "*");
    }

输出:

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

答案 3 :(得分:2)

有一个递归块,白色空间减少。递归在LEFT_SPACE == 0结束。递归块是

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

答案 4 :(得分:2)

以下是一些提示:

  • 你应该横向思考它。
  • 您的主循环必须与楼梯数量相关,因为您受到必须绘制的楼梯数量的约束。
  • 分别打印每一行可能最简单,因此请了解绘制每一行所需的所有信息。

答案 5 :(得分:1)

以下是我最终的结果:

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("*");
    }
}

答案 6 :(得分:1)

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

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(" / \\ *");
}   

}

答案 7 :(得分:0)

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

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(" / \\ *     ");
}
}