Java 打印加号星形图案程序

编写一个使用 for 循环打印加号星形图案的 Java 程序。此 Java 加号星形示例在 for 循环中嵌套 if else 来迭代行。

package ShapePrograms;

import java.util.Scanner;

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

在此 Java 星形加号图案的程序中,我们将for 循环替换为while 循环

package ShapePrograms;

import java.util.Scanner;

public class PlusPattern2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Plus Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Plus Pattern of Stars");
		int i = 1 ;
		while( i <= rows * 2 - 1 ) 
		{
			if (i != rows) 
			{
				int j = 1 ;
				while(j <= rows) 
				{
					if(j == rows) {
						System.out.print("*");
					}
					System.out.print(" ");
					j++;
				}
			}
			else 
			{
				int k = 1;
				while(k <= rows * 2 - 1 ) 
				{
					System.out.print("*");
					k++;
				}
			}
			System.out.println();
			i++;
		}
	}
}
Please Enter Plus Pattern Rows = 7
Printing Plus Pattern of Stars
      * 
      * 
      * 
      * 
      * 
      * 
*************
      * 
      * 
      * 
      * 
      * 
      * 

使用 do while 循环打印加号星形图案的 Java 程序

package ShapePrograms;

import java.util.Scanner;

public class PlusPattern3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Plus Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Plus Pattern of Stars");
		int i = 1 ;
		do
		{
			if (i != rows) 
			{
				int j = 1 ;
				do
				{
					if(j == rows) {
						System.out.print("*");
					}
					System.out.print(" ");
				} while(j++ <= rows);
			}
			else 
			{
				int k = 1;
				do
				{
					System.out.print("*");
				} while(++k <= rows * 2 - 1 );
			}
			System.out.println();
		} while( ++i <= rows * 2 - 1 );
	}
}
Please Enter Plus Pattern Rows = 9
Printing Plus Pattern of Stars
        *  
        *  
        *  
        *  
        *  
        *  
        *  
        *  
*****************
        *  
        *  
        *  
        *  
        *  
        *  
        *  
        * 

在此 Java 示例中,plusPattern 函数打印给定符号的加号图案。

package ShapePrograms;

import java.util.Scanner;

public class PlusPattern4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Plus Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for Plus Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Printing Plus Pattern of Stars");
		plusPattern(rows, ch);	
	}
	
	public static void plusPattern(int rows, char ch) {
		for (int i = 1 ; i <= rows * 2 - 1; i++ ) 
		{
			if (i != rows) 
			{
				for (int j = 1 ; j <= rows; j++ ) 
				{
					if(j == rows) {
						System.out.print(ch);
					}
					System.out.print(" ");
				}
			}
			else 
			{	
				for (int k = 1 ; k <= rows * 2 - 1; k++ ) 
				{
					System.out.print(ch);
				}
			}
			System.out.println();
		}
	}
}
Please Enter Plus Pattern Rows = 11
Enter Character for Plus Pattern = $
Printing Plus Pattern of Stars
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
$$$$$$$$$$$$$$$$$$$$$
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $ 
          $