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

266 阅读1分钟

编写一个Java程序,使用for循环来打印倒置的星形金字塔图案。这个Java倒金字塔图案的例子使用嵌套for循环来迭代和显示输出。

package ShapePrograms;

import java.util.Scanner;

public class InvertedPyramid1 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Inverted Pyramid Pattern Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Printing Inverted Pyramid Star Pattern");
	
	for (int i = rows ; i >= 1; i-- ) 
	{
		for (int j = 0 ; j < rows - i; j++ ) 
		{
			System.out.print(" ");
		}
		for (int k = 0 ; k != (2 \* i) - 1; k++ ) 
		{
			System.out.print("\*");
		}
		System.out.println();
	}
}

image.png

在这个Java倒星金字塔程序中,我们用while循环代替了for循环

package ShapePrograms;

import java.util.Scanner;

public class InvertedPyramid2 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Inverted Pyramid Pattern Rows = ");
	int rows = sc.nextInt();
	

	int i = rows, j, k ;
	while ( i >= 1) 
	{
		j = 0 ;
		while (j < rows - i ) 
		{
			System.out.print(" ");
			j++;
		}
		k = 0 ;
		while ( k != (2 \* i) - 1) 
		{
			System.out.print("\*");
			k++ ;
		}
		System.out.println();
		i-- ;
	}
}
Enter Inverted Pyramid Pattern Rows = 10

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

使用do while循环打印倒置星形金字塔的Java程序

package ShapePrograms;

import java.util.Scanner;

public class InvertedPyramid3 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Inverted Pyramid Pattern Rows = ");
	int rows = sc.nextInt();
	

	int i = rows, j, k ;
	
	do
	{
		j = 0 ;
		do
		{
			System.out.print(" ");
		} while (j++ < rows - i );
		k = 0 ;
		do
		{
			System.out.print("\*");
		} while ( ++k != (2 \* i) - 1);
		System.out.println();
	} while ( --i >= 1) ;
}
Enter Inverted Pyramid Pattern Rows = 13

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

在这个Java例子中,InvertedPyramidPattern函数打印了一个给定符号的倒金字塔图案。

package ShapePrograms;

import java.util.Scanner;

public class InvertedPyramid4 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Inverted Pyramid Pattern Rows = ");
	int rows = sc.nextInt();
	
	System.out.print("Character for Inverted  Pyramid Pattern = ");
	char ch = sc.next().charAt(0);
	

	InvertedPyramidPattern(rows, ch);		
}

public static void InvertedPyramidPattern(int rows, char ch) {
	for (int i = rows ; i >= 1; i-- ) 
	{
		for (int j = 0 ; j < rows - i; j++ ) 
		{
			System.out.print(" ");
		}
		for (int k = 0 ; k != (2 \* i) - 1; k++ ) 
		{
			System.out.print(ch);
		}
		System.out.println();
	}
}
Enter Inverted Pyramid Pattern Rows = 16
Character for Inverted  Pyramid Pattern = $

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
 $$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  $$$$$$$$$$$$$$$$$$$$$$$$$$$
   $$$$$$$$$$$$$$$$$$$$$$$$$
    $$$$$$$$$$$$$$$$$$$$$$$
     $$$$$$$$$$$$$$$$$$$$$
      $$$$$$$$$$$$$$$$$$$
       $$$$$$$$$$$$$$$$$
        $$$$$$$$$$$$$$$
         $$$$$$$$$$$$$
          $$$$$$$$$$$
           $$$$$$$$$
            $$$$$$$
             $$$$$
              $$$
               $