I need help writing a program that prints out two

2020-06-28 16:16发布

Here is what the shapes should look like :

enter image description here

Here is my code so far:

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

I am having trouble getting the second shape

5条回答
一夜七次
2楼-- · 2020-06-28 16:38

By examining the shape on the right, we can notice that for each N asterisks on the line in the left shape, the right one has 10 - N, so, taking your code and extending it, we can get:

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

And if we extract some common code:

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("*");
    }
}
查看更多
Luminary・发光体
3楼-- · 2020-06-28 16:41

You may like that :

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);
    }
 }
}
查看更多
▲ chillily
4楼-- · 2020-06-28 16:41

try this :

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

    }

output :

     *********
      *******
       *****
        ***
         *
        ***
       *****
      *******
     *********
查看更多
兄弟一词,经得起流年.
5楼-- · 2020-06-28 16:43
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();
    }
}
查看更多
\"骚年 ilove
6楼-- · 2020-06-28 16:56

In order to not spoil your fun with this problem, I will explain what you need to do without writing any code.

To get the second shape in there, you would need to add two additional nested for loops into each of the two "outer" loops that you already have.

Loops number three will produce a fixed number of spaces. Note that the distance between the right edge of the first shape and the left edge of the second shape is constant, so your third loops will be easy to code up.

Loops number four will loop like your first loop, but they would change places: the first inner loop from the first outer loop will be the forth inner loop in the second outer loop, and vice versa.

查看更多
登录 后发表回答