难以生产宇宙飞船的身体

时间:2013-10-30 07:17:26

标签: java for-loop

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

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

代码:

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

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

答案 2 :(得分: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进行了交换。