📜  打印倒金字塔星形图案的Java程序

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

打印倒金字塔星形图案的Java程序

方法:

1. 使用 Scanner Class 或 BufferedReader Class 对象从用户获取输入行数。

2. 现在运行两个循环

  • 用于在初始化时迭代多行或从Java的读取器类对象获取输入的外循环。现在,
    • 运行从 1 到 'i-1'内部循环
    • Ru 另一个内循环从1 到行 * 2 – (i × 2 – 1)

插图:

Input: number = 7
 
Output:

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

方法:我们可以使用以下方法打印反向金字塔星形图案:



  1. 使用while循环
  2. 使用 for 循环
  3. 使用 do-while 循环

示例 1:使用 While 循环

Java
// Java program to Print Reverse Pyramid Star Pattern
// Using While loop
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing variable to
        // Size of the pyramid
        int number = 7;
  
        int i = number, j;
  
        // Nested while loops
        // Outer loop
  
        // Till condition holds true
        while (i > 0) {
            j = 0;
  
            // Inner loop
            // Condition check
            while (j++ < number - i) {
                // Print whitespaces
                System.out.print(" ");
            }
  
            j = 0;
  
            // Inner loop
            // Condition check
            while (j++ < (i * 2) - 1) {
                // Print star
                System.out.print("*");
            }
  
            // By now, we reach end of execution for one row
            // so next line
            System.out.println();
  
            // Decrementing counter because we want to print
            // reverse of pyramid
            i--;
        }
    }
}


Java
// Java program to print reverse pyramid star pattern
// Using for loop
import java.io.*;
  
class GFG{
      
public static void main (String[] args) 
{
      
    // Size of the pyramid
    int number = 7;
    int i, j;
      
    // Outer loop handle the number of rows
    for(i = number; i >= 1; i--)
    {
          
        // Inner loop print space
        for(j = i; j < number; j++)
        {
            System.out.print(" ");
        }
          
        // Inner loop print star
        for(j = 1; j <= (2 * i - 1); j++)
        {
            System.out.print("*");
        }
          
        // Ending line after each row
        System.out.println("");
    } 
}
}


Java
// Java program to Print Reverse Pyramid Star Pattern
// Using do-while loop
  
// Importing input output classes
import java.io.*;
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Declare and initialize variable to
        // Size of the pyramid
        int number = 7;
  
        int i = number, j;
  
        // Outer loop iterate until i > 0 is false
        do {
            j = 0;
  
            // First inner do-while loop
            do {
  
                // Prints space until j++ < number - i is
                // false
                System.out.print(" ");
            } while (j++ < number - i);
            j = 0;
  
            // Second inner do-while loop
  
            // Inner loop prints star
            // until j++ < i * 2 - 2 is false
            do {
  
                // print star
                System.out.print("*");
            }
  
            while (j++ < i * 2 - 2);
  
            // Print whitespace
            System.out.println("");
        }
  
        // while of outer 'do-while' loop
        while (--i > 0);
    }
}


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

示例 2:使用 for 循环

Java

// Java program to print reverse pyramid star pattern
// Using for loop
import java.io.*;
  
class GFG{
      
public static void main (String[] args) 
{
      
    // Size of the pyramid
    int number = 7;
    int i, j;
      
    // Outer loop handle the number of rows
    for(i = number; i >= 1; i--)
    {
          
        // Inner loop print space
        for(j = i; j < number; j++)
        {
            System.out.print(" ");
        }
          
        // Inner loop print star
        for(j = 1; j <= (2 * i - 1); j++)
        {
            System.out.print("*");
        }
          
        // Ending line after each row
        System.out.println("");
    } 
}
}
输出
*************
 ***********
  *********
   *******
    *****
     ***
      *

示例 3:使用 do-while 循环

Java

// Java program to Print Reverse Pyramid Star Pattern
// Using do-while loop
  
// Importing input output classes
import java.io.*;
  
// Main Class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Declare and initialize variable to
        // Size of the pyramid
        int number = 7;
  
        int i = number, j;
  
        // Outer loop iterate until i > 0 is false
        do {
            j = 0;
  
            // First inner do-while loop
            do {
  
                // Prints space until j++ < number - i is
                // false
                System.out.print(" ");
            } while (j++ < number - i);
            j = 0;
  
            // Second inner do-while loop
  
            // Inner loop prints star
            // until j++ < i * 2 - 2 is false
            do {
  
                // print star
                System.out.print("*");
            }
  
            while (j++ < i * 2 - 2);
  
            // Print whitespace
            System.out.println("");
        }
  
        // while of outer 'do-while' loop
        while (--i > 0);
    }
}
输出
*************
  ***********
   *********
    *******
     *****
      ***
       *