打印右箭星图案的Java程序

222 阅读2分钟

编写一个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右箭头星形图案程序中,我们用一个while循环代替了for循环

package ShapePrograms;

import java.util.Scanner;

public class RightArrow2 { private static Scanner scanner;

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 ------
******
 *****
  ****
   ***
    **
     *
    **
   ***
  ****
 *****
******

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

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 scanner;

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 ----
##########
 #########
  ########
   #######
    ######
     #####
      ####
       ###
        ##
         #
        ##
       ###
      ####
     #####
    ######
   #######
  ########
 #########
##########