-1

我无法生成正确的输出。我已经制作了身体的顶部,但无法得到底部。它几乎是它的翻转版本。这是我想要得到的输出:

|../\..../\..|
|./\/\../\/\.|
|/\/\/\/\/\/\|
|\/\/\/\/\/\/|
|.\/\/..\/\/.|
|..\/....\/..|

代码:

public static void Body() {
    for (int f = 1; f <= 3; f++) {
        System.out.print("|");

        for (int g = 1; g <= 3 - f; g++) {
            System.out.print(".");
        }            

        for (int h = 1; h <= f; h++) {
            System.out.print("/\\");
        }

        for (int i = 1; i <= 3-f; i++) {
            System.out.print("..");
        }

        for (int j = 1; j <= f; j++) {
            System.out.print("/\\");
        }

        for (int h = 1; h <= 3 - f; h++) {
            System.out.print(".");
        }

        System.out.print("|");
        System.out.println();      
    }
}

public static void Body2() {
    for (int i = 1; i <= 3; i++) {
        System.out.print("|");

        for (int j = 1; j <= 3 - i; j++) {
            System.out.print(".");
        }            

        for (int k = 1; k <= i; k++) {
            System.out.print("\\/");
        }

        for (int l = 1; l <= 3-i; l++) {
            System.out.print("..");
        }

        for (int m = 1; m <= i; m++) {
            System.out.print("\\/");
        }

        for (int n = 1; n <= 3 - i; n++) {
            System.out.print(".");
        }

        System.out.print("|");
        System.out.println();      
    }
}
4

3 回答 3

1

您需要在方法 from中迭代外for循环以获得所需的输出。您不需要更改方法的任何内部循环:body2()i = 3 to i > 0forbody2()

public static void body2() {
    for (int i = 3; i > 0; i--) {
        System.out.print("|");

        for (int j = 1; j <= 3 - i; j++) {
            System.out.print(".");
        }

        for (int k = 1; k <= i; k++) {
            System.out.print("\\/");
        }

        for (int l = 1; l <= 3 - i; l++) {
            System.out.print("..");
        }

        for (int m = 1; m <= i; m++) {
            System.out.print("\\/");
        }

        for (int n = 1; n <= 3 - i; n++) {
            System.out.print(".");
        }

        System.out.print("|");
        System.out.println();
    }
}
于 2013-10-30T07:59:00.483 回答
0

看来您的静态方法 Body 和 Body2 正在做同样的事情。你想让 Body2 成为 Body 的翻转版本吗?如果是这样,您可以更改它输出的字符串以使其正确,非常简单。

于 2013-10-30T07:22:42.577 回答
0

试一试,我还不能测试它:

public static void Body2() {
    for (int i = 1; i <= 3; i++) {
        System.out.print("|");

        for (int j = 1; j <= i; j++) {
            System.out.print(".");
        }            

        for (int k = 1; k <= 3 - i; k++) {
            System.out.print("\\/");
        }

        for (int l = 1; l <= i; l++) {
            System.out.print("..");
        }

        for (int m = 1; m <= i; m++) {
            System.out.print("\\/");
        }

        for (int n = 1; n <= i; n++) {
            System.out.print(".");
        }

        System.out.print("|");
        System.out.println();      
    }

我基本上只是交换了你3 - ii和你i3 - i

于 2013-10-30T07:24:09.667 回答