27

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

在此处输入图像描述

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

这是我不完整的代码:

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

            }
        }
    }
}
4

8 回答 8

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

于 2012-07-23T07:28:01.300 回答
4

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

  • 如有必要,空间与火柴人的数量成比例
  • 火柴人的一部分 - 你可以硬编码
  • 6 的平面*,或 1*后跟 5 个空格
  • 如有必要,空间与火柴人的数量成比例
  • 一个*

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

于 2012-07-23T07:21:23.000 回答
3

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

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  *****                    *
  /|\ *                        *
  / \ *                        *
于 2012-07-23T09:57:41.213 回答
2

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

LEFT_SPACE o  ******RIGHT_SPACE*
LEFT_SPACE/|\ *     RIGHT_SPACE*
LEFT_SPACE/ \ *     RIGHT_SPACE*
于 2012-07-23T07:30:38.333 回答
2

以下是一些提示:

  • 你应该横向考虑。
  • 您的主循环必须与楼梯数量相关,因为您受到必须绘制的楼梯数量的约束。
  • 单独打印每一行可能是最简单的,因此请了解绘制每一行所需的所有信息。
于 2012-07-23T07:33:07.463 回答
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("*");
    }
}
于 2012-07-23T10:37:50.317 回答
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(" / \\ *");
}   

}

于 2016-02-21T18:08:42.657 回答
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(" / \\ *     ");
}
}
于 2015-10-17T05:12:30.003 回答