打印反向镜像直角三角形星形图案的Java程序

105 阅读1分钟

编写一个Java程序,使用for循环打印反向镜像的直角三角形星形图案。这个Java反向镜像直角三角形例子根据If条件打印星形或空位。

package ShapePrograms;

import java.util.Scanner;

public class RevMirroredRightTri1 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Reverse Mirrored Right Triangle Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Reverse Mirrored Right Triangle Star Pattern");
	
	for (int i = 1 ; i <= rows; i++ ) 
	{
		for (int j = 1 ; j <= rows; j++ ) 
		{
			if(j < i)
			{
				System.out.print(" ");
			}
			else
			{
				System.out.print("\*");
			}
		}
		System.out.println();
	}
}

image.png

在这个Java反向镜像的右三角星形图案程序中,我们用while循环代替了for循环

package ShapePrograms;

import java.util.Scanner;

public class RevMirroredRightTri2 { private static Scanner sc;

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

	int i = 1, j;
	while( i <= rows ) 
	{
		j = 1 ;
		while(j <= rows) 
		{
			if(j < i)
			{
				System.out.print(" ");
			}
			else
			{
				System.out.print("\*");
			}
			j++;
		}
		System.out.println();
		i++;
	}
}
Enter Reverse Mirrored Right Triangle Rows = 10

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

使用do while循环打印反向镜像右三角星形图案的Java程序

package ShapePrograms;

import java.util.Scanner;

public class RevMirroredRightTri3 { private static Scanner sc;

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

	int i = 1, j;
	do
	{
		j = 1 ;
		do 
		{
			if(j < i)
			{
				System.out.print(" ");
			}
			else
			{
				System.out.print("\*");
			}
		} while(++j <= rows);
		System.out.println();
	} while( ++i <= rows );
}
Enter Reverse Mirrored Right Triangle Rows = 14

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

在这个Java例子中,RevMirroredRightTriangle函数打印了一个给定符号的反向镜像右三角形图案。

package ShapePrograms;

import java.util.Scanner;

public class RevMirroredRightTri4 { private static Scanner sc;

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

	RevMirroredRightTriangle(rows, ch);		
}

public static void RevMirroredRightTriangle(int rows, char ch) {
	for (int i = 1 ; i <= rows; i++ ) 
	{
		for (int j = 1 ; j <= rows; j++ ) 
		{
			if(j < i)
			{
				System.out.print(" ");
			}
			else
			{
				System.out.print(ch);
			}
		}
		System.out.println();
	}
}
Enter Reverse Mirrored Right Triangle Rows = 17
Character for Reverse Mirrored Right Triangle Pattern = #

#################
 ################
  ###############
   ##############
    #############
     ############
      ###########
       ##########
        #########
         ########
          #######
           ######
            #####
             ####
              ###
               ##
                #