Java 打印右箭头星形图案程序

编写一个 Java 程序,使用 for 循环打印右箭头星形图案。此 Java 右箭头星形示例将代码分为两部分,第一部分(嵌套 for 循环)打印右箭头的上半部分,另一部分代码打印下半部分。

package ShapePrograms;

import java.util.Scanner;

public class RightArrow1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j;
		
		System.out.print("Please Enter Right Arrow Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Right Arrow Pattern of Stars");
		
		for (i = 0 ; i < rows; i++ ) 
		{
			for (j = 0 ; j < rows; j++ ) 
			{
				if(j < i) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
			}
			System.out.println();
		}
		
		for (i = 2 ; i <= rows; i++ ) 
		{
			for (j = 0 ; j < rows; j++ ) 
			{
				if(j < rows - i) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print Right Arrow Star Pattern

在这个 Java 右箭头星形图案 程序中,我们将 for 循环替换为 while 循环

package ShapePrograms;

import java.util.Scanner;

public class RightArrow2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j;
		
		System.out.print("Please Enter Right Arrow Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("---- Printing Right Arrow Pattern of Stars ------");
		i = 0 ;
		while ( i < rows ) {
			j = 0 ;
			while( j < rows) {
				if(j < i) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
				j++;
			}
			System.out.println();
			i++;
		}
		i = 2 ;
		while( i <= rows ) 
		{
			j = 0 ;
			while( j < rows) 
			{
				if(j < rows - i) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
				j++;
			}
			System.out.println();
			i++;
		}
	}
}
Please Enter Right Arrow Pattern Rows = 6
---- Printing Right Arrow Pattern of Stars ------
******
 *****
  ****
   ***
    **
     *
    **
   ***
  ****
 *****
******

Java 使用 do while 循环打印右箭头星形图案的程序

package ShapePrograms;

import java.util.Scanner;

public class RightArrow3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j;
		
		System.out.print("Please Enter Right Arrow Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("---- Printing Right Arrow Pattern of Stars ------");
		i = 0 ;
		do {
			j = 0 ;
			do {
				if(j < i) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
				j++;
			} while( j < rows);
			System.out.println();
			i++;
		} while ( i < rows );
		
		i = 2 ;
		do	{
			j = 0 ;
			do	{
				if(j < rows - i) {
					System.out.print(" ");
				}
				else {
					System.out.print("*");
				}
				j++;
			} while( j < rows) ;
			System.out.println();
			i++;
		} while( i <= rows );
	}
}
Please Enter Right Arrow Pattern Rows = 8
---- Printing Right Arrow Pattern of Stars ------
********
 *******
  ******
   *****
    ****
     ***
      **
       *
      **
     ***
    ****
   *****
  ******
 *******
********

在此 Java 示例中,RightArrowPattern 函数打印给定符号的右箭头图案。

package ShapePrograms;

import java.util.Scanner;

public class RightArrow4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Right Arrow Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Please Enter Character for Right Arrow Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("--- Printing Right Arrow Pattern of Stars ----");
		RightArrowPattern(rows, ch);
	}
	
	public static void RightArrowPattern(int rows, char ch) {
		int i, j;
		for (i = 0 ; i < rows; i++ ) 
		{
			for (j = 0 ; j < rows; j++ ) 
			{
				if(j < i) {
					System.out.print(" ");
				}
				else {
					System.out.print(ch);
				}
			}
			System.out.println();
		}
		
		for (i = 2 ; i <= rows; i++ ) 
		{
			for (j = 0 ; j < rows; j++ ) 
			{
				if(j < rows - i) {
					System.out.print(" ");
				}
				else {
					System.out.print(ch);
				}
			}
			System.out.println();
		}
	}
}
Please Enter Right Arrow Pattern Rows = 10
Please Enter Character for Right Arrow Pattern = #
--- Printing Right Arrow Pattern of Stars ----
##########
 #########
  ########
   #######
    ######
     #####
      ####
       ###
        ##
         #
        ##
       ###
      ####
     #####
    ######
   #######
  ########
 #########
##########