我需要帮助编写一个程序,使用嵌套循环在一行上打印出两个形状

时间:2014-10-02 16:47:27

标签: java loops nested-loops

以下是形状的外观like

enter image description here

到目前为止,这是我的代码:

public class Diamonds {

    public static void main(String[] args) {
        for (int i = 1; i < 10; i += 2) {
            for (int j = 0; j < 9 - i / 2; j++) {
                System.out.print(" ");
            }

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

            System.out.print("\n");
        }

        for (int i = 7; i > 0; i -= 2) {
            for (int j = 0; j < 9 - i / 2; j++) {
                System.out.print(" ");
            }

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

            System.out.print("\n");
        }
    }
}

我无法获得第二个形状

5 个答案:

答案 0 :(得分:1)

为了不破坏这个问题的乐趣,我将在不编写任何代码的情况下解释您需要做什么。

要获得第二个形状,您需要在已有的两个“外部”循环中的每一个中添加两个额外的嵌套for循环。

第三个循环将产生固定数量的空格。请注意,第一个形状的右边缘和第二个形状的左边缘之间的距离是恒定的,因此您的第三个循环将很容易编码。

第四个循环将像第一个循环一样循环,但它们会改变位置:第一个外循环的第一个内循环将是第二个外循环中的第四个内循环,反之亦然。

答案 1 :(得分:0)

通过检查右侧的形状,我们可以注意到,对于左侧形状的线上的每个N星号,右侧的星号为10 - N,因此,接受您的代码并扩展它,我们可以得到:

public class Diamonds {
    public static final String SPACE = "        ";
    public static void main(String[] args) {
        for (int i = 1; i < 10; i += 2) {
            for (int j = 0; j < 9 - i / 2; j++)
                System.out.print(" ");

            for (int j = 0; j < i; j++)
                System.out.print("*");

            System.out.print(SPACE);

            for (int j = 0; j < 10 - i; j++)
                System.out.print("*");

            System.out.print("\n");
        }

        for (int i = 7; i > 0; i -= 2) {
            for (int j = 0; j < 9 - i / 2; j++)
                System.out.print(" ");

            for (int j = 0; j < i; j++)
                System.out.print("*");

            System.out.print(SPACE);

            for (int j = 0; j < 10 - i; j++)
                System.out.print("*");

            System.out.print("\n");
        }
    }
}

如果我们提取一些常用代码:

public class Diamonds {
    public static final String SPACE = "        ";
    public static void main(String[] args) {
        for (int i = 1; i < 10; i += 2) {
            drawLine(i);

            System.out.print("\n");
        }

        for (int i = 7; i > 0; i -= 2) {
            drawLine(i);

            System.out.print("\n");
        }
    }

    private static void drawLine(int i) {
        for (int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");

        for (int j = 0; j < i; j++)
            System.out.print("*");

        System.out.print(SPACE);

        for (int j = 0; j < 10 - i; j++)
            System.out.print("*");
    }
}

答案 2 :(得分:0)

试试这个:

public static void main(String[] args) {

        for (int i = 9; i > 0; i -= 2) {
            for (int j = 0; j < 9 - i / 2; j++)
                System.out.print(" ");

            for (int j = 0; j < i; j++)
                System.out.print("*");

            System.out.print("\n");
        }
        for (int i = 2; i < 10; i += 2) {
            for (int j = 0; j < 9 - i / 2; j++)
                System.out.print(" ");

            for (int j = 0; j <= i; j++)
                System.out.print("*");

            System.out.print("\n");
        }

    }

输出:

     *********
      *******
       *****
        ***
         *
        ***
       *****
      *******
     *********

答案 3 :(得分:0)

public class ReverseDiamond {

    public static void main(String[] ar) {

        int rows = 10;
        ReverseDiamond diamond = new ReverseDiamond();          

        for(int i = 0; i < rows; i++)
            diamond.printLine(rows, i); 

        for(int i = rows - 2; i >= 0; i--)
            diamond.printLine(rows, i);         
    }



    private void printLine(int rows, int currRow) {

        for(int space = 1; space <= currRow; space++)
            System.out.print(" ");

        for(int star = 1; star < 2 * (rows - currRow); star++)
            System.out.print("*");

        System.out.println();
    }
}

答案 4 :(得分:0)

您可能喜欢这样:

public class Diamonds {
 public static void main(String[] args) {
    int totalStars = 9;
    int rows = 9;

    for (int r = 0,stars=-1,gap=totalStars; r < rows; r++   ) {
        stars+= (r<=rows/2) ?2:-2;
        gap=totalStars-stars;

        printChars(' ', gap);
        printChars('*', stars);
        printChars(' ', gap);
        int gap2=stars+1;
        int stars2=gap+1;
        printChars(' ', gap2);
        printChars('*', stars2);
        printChars(' ', gap2);
        System.out.println();
    }
 }

 private static void printChars(char c ,int times) {
    for (int i = 0; i < times; i++) {
        System.out.print(c);
    }
 }
}