📜  打印方形星形图案的Java程序

📅  最后修改于: 2022-05-13 01:55:25.467000             🧑  作者: Mango

打印方形星形图案的Java程序

在这里,我们将实现一个Java程序来打印方形星形图案。我们将打印带对角线和不带对角线的方形星形图案。

例子:

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

方法:

第一步:输入行数和列数。

第 2 步:对于矩形的行,从 1 到行运行外部循环。



for (i = 1; i < = rows; i++)

第 3 步:对于矩形的列,从 1 到列运行内循环。

for (j = 1; j < = columns; j++)

第 4 步:在第一行或最后一行或第一列或最后一列打印星号,否则打印空格。

第 5 步:打印一行的所有列后,打印 内循环后的换行符。

示例 1:无对角线的星形方形图案

Java
// Java Program to Print Square Pattern
// Case 1: Hollow rectangle
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // To print hollow rectangle
    static void print_rectangle(int k, int l)
    {
        int a, b;
 
        // Nested for loops for iterations
 
        // Outer loop for rows
        for (a = 1; a <= k; a++) {
            // Inner loop for columns
            for (b = 1; b <= l; b++) {
                // Condition check using logical OR operator
                // over rows and columns positions
                // if found at circumference of rectangle
                if (a == 1 || a == k || b == 1 || b == l)
 
                    // Print the star pattern
                    System.out.print("*");
                else
 
                    // Rest inside square print the empty
                    // spaces
                    System.out.print(" ");
            }
 
            // By now we are done with only 1 row so
            // New line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // Declaring and initializing rows and columns
        // For square row = columns
 
        // Custom input initialization values
        int rows = 8, columns = 22;
 
        // Calling the method1 to print hollow rectangle
        // pattern
        print_rectangle(rows, columns);
    }
}


Java
// Java Program to Print Square Star pattern
// Case: Primary and secondary diagonal Rectangle Pattern
import java.io.*;
 
// Mai class
class GFG {
 
    // Method 1
    // To print square with primary and secondary diagonal
    static void print_squaredi(int k)
    {
        int a, b;
 
        // Nested 2 for loops for Matrix rinting
 
        // Outer loop for rows
        for (a = 1; a <= k; a++) {
            // Inner loop for columns
            for (b = 1; b <= k; b++) {
                // Condition check using OR operator where
                // 1. Printing star as per first 4 checks
                // on the circumference of rectangle
                // 2. Fifth check is for diagonals
                if (a == 1 || a == k || b == 1 || b == k
                    || a == b || b == (k - a + 1))
 
                    // Print the star pattern
                    System.out.print("*");
                else
 
                    // Print the white spaces
                    System.out.print(" ");
            }
 
            // By now we are over with one row so
            // new line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // This time taking square so only one variable
        // needs to be declared
        // Custom input entry
        int rows = 12;
 
        // calling the method1 to print
        // square pattern with diagonal
        print_squaredi(rows);
    }
}



输出
**********************
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
**********************

现在更进一步,我们只是在矩形内插入空格。在数学中,我们已经遇到过对角线,所以我们也可以在上面的例子中打印它们。



示例 2:带对角线的星形方形图案

Java

// Java Program to Print Square Star pattern
// Case: Primary and secondary diagonal Rectangle Pattern
import java.io.*;
 
// Mai class
class GFG {
 
    // Method 1
    // To print square with primary and secondary diagonal
    static void print_squaredi(int k)
    {
        int a, b;
 
        // Nested 2 for loops for Matrix rinting
 
        // Outer loop for rows
        for (a = 1; a <= k; a++) {
            // Inner loop for columns
            for (b = 1; b <= k; b++) {
                // Condition check using OR operator where
                // 1. Printing star as per first 4 checks
                // on the circumference of rectangle
                // 2. Fifth check is for diagonals
                if (a == 1 || a == k || b == 1 || b == k
                    || a == b || b == (k - a + 1))
 
                    // Print the star pattern
                    System.out.print("*");
                else
 
                    // Print the white spaces
                    System.out.print(" ");
            }
 
            // By now we are over with one row so
            // new line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // This time taking square so only one variable
        // needs to be declared
        // Custom input entry
        int rows = 12;
 
        // calling the method1 to print
        // square pattern with diagonal
        print_squaredi(rows);
    }
}


输出
************
**        **
* *      * *
*  *    *  *
*   *  *   *
*    **    *
*    **    *
*   *  *   *
*  *    *  *
* *      * *
**        **
************