返回

Shape Printing in Java: Explore Creative Patterns

闲谈

Embarking on Our Shape Printing Journey

In the realm of Java programming, we possess the power to manipulate data and create intricate patterns. One fascinating application of this is shape printing, where we can use code to generate visually appealing shapes using characters like asterisks (*).

Rectangle: A Foundation for Shape Printing

Let's start with the classic rectangle. To print a rectangle in Java, we'll use nested loops to control the rows and columns. Here's an example:

public class RectanglePrinter {

    public static void main(String[] args) {
        int rows = 4;
        int columns = 9;

        // Outer loop for rows
        for (int i = 0; i < rows; i++) {
            // Inner loop for columns
            for (int j = 0; j < columns; j++) {
                System.out.print("* ");
            }

            // Move to the next line
            System.out.println();
        }
    }
}

Running this program will print a 4x9 rectangle made of asterisks.

Triangle: A Journey from Base to Apex

Next, let's tackle the triangle. To create a right triangle, we'll use a loop to control the number of rows and spaces. Here's how:

public class TrianglePrinter {

    public static void main(String[] args) {
        int rows = 5;

        // Outer loop for rows
        for (int i = 0; i < rows; i++) {
            // Inner loop for spaces
            for (int j = rows - i; j > 1; j--) {
                System.out.print(" ");
            }

            // Inner loop for asterisks
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }

            // Move to the next line
            System.out.println();
        }
    }
}

This program will print a right triangle with a base of 5 asterisks.

Rhombus: A Diamond in the Rough

Finally, let's create a rhombus, also known as a diamond. We'll combine the techniques used for the rectangle and triangle to achieve this:

public class RhombusPrinter {

    public static void main(String[] args) {
        int rows = 5;

        // Upper half of the rhombus
        for (int i = 0; i < rows; i++) {
            // Inner loop for spaces
            for (int j = rows - i; j > 1; j--) {
                System.out.print(" ");
            }

            // Inner loop for asterisks
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }

            // Move to the next line
            System.out.println();
        }

        // Lower half of the rhombus
        for (int i = rows - 1; i >= 0; i--) {
            // Inner loop for spaces
            for (int j = rows - i; j > 1; j--) {
                System.out.print(" ");
            }

            // Inner loop for asterisks
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }

            // Move to the next line
            System.out.println();
        }
    }
}

This program will print a rhombus with a height and width of 5 asterisks.

Conclusion: A World of Shapes Awaits

In this article, we explored the art of shape printing in Java, creating rectangles, triangles, and rhombuses using code. These examples serve as a foundation for further exploration into more complex shapes and patterns. The possibilities are endless, so let your creativity soar and continue your journey into the captivating realm of shape printing!