BasdGod
BasdGod

Reputation: 33

I need help writing a program that prints out two shapes on one line using nested loops

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

Upvotes: 1

Views: 9139

Answers (5)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

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.

Upvotes: 1

Rustam
Rustam

Reputation: 6515

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 :

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

Upvotes: 0

sotcha
sotcha

Reputation: 923

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

Upvotes: 0

Aditya Singh
Aditya Singh

Reputation: 2443

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

Upvotes: 0

Mureinik
Mureinik

Reputation: 311163

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

Upvotes: 0

Related Questions