📜  打印镜像上星三角形图案的Java程序

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

打印镜像上星三角形图案的Java程序

该图案有两个部分——彼此的镜面反射。三角形的底部必须在假想镜子的底部,尖端在顶部。

插图:

Input:
size = 7

Output:

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

方法:

将模式划分为子模式

  • 把这个分成两部分
    • 上面的部分
    • 较低的部分
  • 两部分是彼此的镜面反射。每个部分由2个三角形组成。因此,总的来说,我们必须打印 4 个三角形才能获得所需的图案。



示例 1:上半部分

Java
// Java Program to Print Upper Part of Mirror Upper Star
// Triangle Pattern
 
// Importing input output classes
import java.io.*;
 
// Main Class
class GFG {
 
    // Method 1
    // To print upper part of the pattern
    private static void displayUpperPart(int size)
    {
 
        // Declaring variables for rows and columns
        // respectively
        int m, n;
 
        // Outer loop for rows
        for (m = size - 1; m >= 0; m--) {
 
            // Inner loop 1
            // to print triangle 1
            for (n = 0; n < m; n++) {
 
                // Printing whitespace
                System.out.print(" ");
            }
 
            // Inner loop 2
            // to print triangle 2
            for (n = m; n <= size - 1; n++) {
 
                // Printing star with whitespace
                System.out.print("*"
                                 + " ");
            }
 
            // By now done with one rpw so next line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variables to
        // size of the triangle
        int size = 7;
 
        // Calling the above Method 1 to
        // print and display the upper part of triangle
        displayUpperPart(size);
    }
}


Java
// Java Program to Print Lower Part of Mirror Upper Star
// Triangle Pattern
 
// Importing input output classes
import java.io.*;
 
// Main Class
class GFG {
 
    // Method 1
    // To print lower part of the pattern
    private static void displayLowerPart(int size)
    {
 
        // Declaring variables for rows and columns
        // respectively
        int m, n;
 
        // Outer loop fo Rows
        for (m = 1; m <= size; m++) {
 
            // Inner loop 1 to print triangle 3
            for (n = 1; n < m; n++) {
 
                // Printing whitespace
                System.out.print(" ");
            }
 
            // Inner loop 2 to print triangle 4
            for (n = m; n <= size; n++) {
 
                // Printing star and whitespace
                System.out.print("*"
                                 + " ");
            }
 
            // By now done with one row so new line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variable to
        // size of the triangle
        // This is number of rows
        int size = 7;
 
        // Calling the above Method1
        // to display lower part of the triangle
        displayLowerPart(size);
    }
}


Java
// Java Program to Print Mirror upper Star triangle Pattern
 
// Importing input output classes
import java.io.*;
 
// Main Class
public class GFG {
 
    // Method 1
    // To print upper part of the pattern
    private static void displayUpperPart(int size)
    {
 
        // Declaring variables for rows and columns
        // respectively
        int m, n;
 
        // Outer loop for rows
        for (m = size - 1; m >= 0; m--) {
 
            // Inner loop 1
            for (n = 0; n < m; n++) {
 
                // Printing whitespace
                System.out.print(" ");
            }
 
            // Inner loop 2
            for (n = m; n <= size - 1; n++) {
 
                // Printing star with whitespace
                System.out.print("*"
                                 + " ");
            }
 
            // By now we are done with one row so new line
            System.out.println();
        }
    }
 
    // Method 2
    // To print lower part of the pattern
    private static void displayLowerPart(int size)
    {
 
        // Declaring variables for rows and columns
        // respectively
        int m, n;
 
        // Outer loop for rows
        for (m = 1; m <= size; m++) {
 
            // Inner loop 1
            for (n = 1; n < m; n++) {
 
                // Printing whitespace
                System.out.print(" ");
            }
 
            // Inner loop 2
            for (n = m; n <= size; n++) {
 
                // Printing star and whitespace
                System.out.print("*"
                                 + " ");
            }
 
            // By now we are done with one row so new line
            System.out.println();
        }
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variable to
        // size of the triangle
        int size = 7;
 
        // Calling Method 1 to
        // display the upper part
        displayUpperPart(size);
 
        // Calling Method 2 to
        // display lower part
        displayLowerPart(size);
    }
}



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

示例 2:三角形的下半部分

Java

// Java Program to Print Lower Part of Mirror Upper Star
// Triangle Pattern
 
// Importing input output classes
import java.io.*;
 
// Main Class
class GFG {
 
    // Method 1
    // To print lower part of the pattern
    private static void displayLowerPart(int size)
    {
 
        // Declaring variables for rows and columns
        // respectively
        int m, n;
 
        // Outer loop fo Rows
        for (m = 1; m <= size; m++) {
 
            // Inner loop 1 to print triangle 3
            for (n = 1; n < m; n++) {
 
                // Printing whitespace
                System.out.print(" ");
            }
 
            // Inner loop 2 to print triangle 4
            for (n = m; n <= size; n++) {
 
                // Printing star and whitespace
                System.out.print("*"
                                 + " ");
            }
 
            // By now done with one row so new line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variable to
        // size of the triangle
        // This is number of rows
        int size = 7;
 
        // Calling the above Method1
        // to display lower part of the triangle
        displayLowerPart(size);
    }
}


输出

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

示例 3:完整的镜像上星三角形图案

Java

// Java Program to Print Mirror upper Star triangle Pattern
 
// Importing input output classes
import java.io.*;
 
// Main Class
public class GFG {
 
    // Method 1
    // To print upper part of the pattern
    private static void displayUpperPart(int size)
    {
 
        // Declaring variables for rows and columns
        // respectively
        int m, n;
 
        // Outer loop for rows
        for (m = size - 1; m >= 0; m--) {
 
            // Inner loop 1
            for (n = 0; n < m; n++) {
 
                // Printing whitespace
                System.out.print(" ");
            }
 
            // Inner loop 2
            for (n = m; n <= size - 1; n++) {
 
                // Printing star with whitespace
                System.out.print("*"
                                 + " ");
            }
 
            // By now we are done with one row so new line
            System.out.println();
        }
    }
 
    // Method 2
    // To print lower part of the pattern
    private static void displayLowerPart(int size)
    {
 
        // Declaring variables for rows and columns
        // respectively
        int m, n;
 
        // Outer loop for rows
        for (m = 1; m <= size; m++) {
 
            // Inner loop 1
            for (n = 1; n < m; n++) {
 
                // Printing whitespace
                System.out.print(" ");
            }
 
            // Inner loop 2
            for (n = m; n <= size; n++) {
 
                // Printing star and whitespace
                System.out.print("*"
                                 + " ");
            }
 
            // By now we are done with one row so new line
            System.out.println();
        }
    }
 
    // Method 3
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variable to
        // size of the triangle
        int size = 7;
 
        // Calling Method 1 to
        // display the upper part
        displayUpperPart(size);
 
        // Calling Method 2 to
        // display lower part
        displayLowerPart(size);
    }
}


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