使用for循环缩放输出

时间:2014-02-26 01:14:59

标签: java math for-loop

我要做的是制作一个盒子,如下:

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

并且代码是

public static void drawUpper(int count)
{
    System.out.print("|");

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

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

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

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

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

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

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

public static void drawLower(int count)
{
    System.out.print("|");

    for(int i = 0; i <= -count; i++){
        System.out.print(".");
    }

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

    for(int i = 0; i <= -count; i++){
        System.out.print(".");
    }

    for(int i = 0; i <= -count; i++){
        System.out.print(".");
    }

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

    for(int i = 0; i <= -count; i++){
        System.out.print(".");
    }

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

然而,这需要达到规模,目前的规模是5;

private static int size = 5;


public static void main(String[] args)
{
    for(int i = 1; i > (-size / 2); i--){
        drawUpper(i);
    }

    for(int i = 1; i > (-size / 2); i--){
        drawLower(i);
    }
}

现在,只要规模不变,它就能完美运行,我对自己很满意。然而,当我将比例改为7时,框似乎变成了这个:

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

然而,它应该是这样的:

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

1 个答案:

答案 0 :(得分:1)

这就是我想出的。我只提供了制作一颗钻石的代码(我不会为你完成所有的工作;))。将它调整为生产两颗钻石是微不足道的,所以由你自己做出这些改变(根据下面的代码,它真的不难)。

// aliases for java's verbose methods
public static void p(char c) { System.out.print(c); }
public static void pn(char c) { System.out.println(c); }

// convenient shortcut for alternating between characters
public static char[] a = {'/', '\\'};

public static void dotop(int size, int line) {
    p('|');
    for (int i=0; i<size-line; i++) p('.');
    for (int i=0; i<line+1; i++) p(a[i%2]);
    for (int i=0; i<line+1; i++) p(a[(i+1+line)%2]);
    for (int i=0; i<size-line; i++) p('.');
    pn('|');
}

public static void dobottom(int size, int line) {
    p('|');
    for (int i=0; i<size-line; i++) p('.');
    for (int i=0; i<line+1; i++) p(a[(i+line+(line+1)%2)%2]);
    for (int i=0; i<line+1; i++) p(a[(i+1+(line+1)%2)%2]);
    for (int i=0; i<size-line; i++) p('.');
    pn('|');
}

public static void main(String[] args) {
    int size = 4;
    for (int i=0; i<=size; i++) dotop(size, i);
    for (int i=0; i<=size; i++) dobottom(size, size-i);
}

产生的输出是:

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