Java 打印 H 星形图案程序

编写一个 Java 程序,使用 for 循环打印 H 星形图案。

package Shapes3;

import java.util.Scanner;

public class HStarPattern1 {

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

此 Java 程序使用 while 循环显示星号组成的字母 H 图案。

package Shapes3;

import java.util.Scanner;

public class HStarPattern2 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k, l;
		
		System.out.print("Please Enter H Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing H Star Pattern");
		i = 1;
		while( i <= rows ) 
		{
			j = 1;
			while(j <= i) 
			{
				System.out.print("*");
				j++;
			}
			k = i * 2;
			while( k <= rows * 2 - 1) 
			{
				System.out.print(" ");
				k++;
			}
			l = 1;
			while( l <= i ) 
			{
				System.out.print("*");
				l++;
			}
			System.out.println();
			i++;
		}
		
		i = 1;
		while (i <= rows - 1 ) 
		{
			j = rows - 1;
			while ( j >= i ) 
			{
				System.out.print("*");
				j--;
			}
			k = 1;
			while( k <= i * 2) 
			{
				System.out.print(" ");
				k++;
			}
			l = rows - 1; 
			while (l >= i ) 
			{
				System.out.print("*");
				l--;
			}
			System.out.println();
			i++;
		}
	}
}
Please Enter H Pattern Rows = 11
Printing H Star Pattern
*                    *
**                  **
***                ***
****              ****
*****            *****
******          ******
*******        *******
********      ********
*********    *********
**********  **********
**********************
**********  **********
*********    *********
********      ********
*******        *******
******          ******
*****            *****
****              ****
***                ***
**                  **
*                    *

此 Java 程序使用 do while 循环打印星号组成的 H 图案。

package Shapes3;

import java.util.Scanner;

public class HStarPattern3 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k, l;
		
		System.out.print("Please Enter H Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing H Star Pattern");
		i = 1;
		do
		{
			j = 1;
			do
			{
				System.out.print("*");
			} while(++j <= i);
			k = i * 2;
			while( k <= rows * 2 - 1) 
			{
				System.out.print(" ");
				k++;
			}
			l = 1;
			do
			{
				System.out.print("*");
			} while( ++l <= i );
			System.out.println();
		} while( ++i <= rows ) ;
		
		i = 1;
		do
		{
			j = rows - 1;
			do
			{
				System.out.print("*");
			} while (--j >= i );
			k = 1;
			do
			{
				System.out.print(" ");
			} while(++k <= i * 2);
			l = rows - 1; 
			do 
			{
				System.out.print("*");
			} while (--l >= i );
			System.out.println();
		} while (++i <= rows - 1 );
	
	}
}
Please Enter H Pattern Rows = 13
Printing H Star Pattern
*                        *
**                      **
***                    ***
****                  ****
*****                *****
******              ******
*******            *******
********          ********
*********        *********
**********      **********
***********    ***********
************  ************
**************************
************  ************
***********    ***********
**********      **********
*********        *********
********          ********
*******            *******
******              ******
*****                *****
****                  ****
***                    ***
**                      **
*                        *

在此 Java 示例中,HPattern 函数允许用户输入字符并打印给定字符的 H 图案。

package Shapes3;

import java.util.Scanner;

public class HStarPattern4 {

	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter H Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for H Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Printing H Pattern");
		HPattern(rows, ch);
		
	}
	public static void HPattern(int rows, char ch) {
		int i, j, k, l;
		
		for (i = 1; i <= rows; i++ ) 
		{
			for (j = 1; j <= i; j++ ) 
			{
				System.out.print(ch);
			}
			for(k = i * 2; k <= rows * 2 - 1; k++) 
			{
				System.out.print(" ");
			}
			for (l = 1; l <= i; l++ ) 
			{
				System.out.print(ch);
			}
			System.out.println();
		}
		
		for (i = 1; i <= rows - 1; i++ ) 
		{
			for (j = rows - 1; j >= i; j-- ) 
			{
				System.out.print(ch);
			}
			for(k = 1; k <= i * 2; k++) 
			{
				System.out.print(" ");
			}
			for (l = rows - 1; l >= i; l-- ) 
			{
				System.out.print(ch);
			}
			System.out.println();
		}
	}
}
Please Enter H Pattern Rows = 17
Enter Character for H Pattern = $
Printing H Pattern
$                                $
$$                              $$
$$$                            $$$
$$$$                          $$$$
$$$$$                        $$$$$
$$$$$$                      $$$$$$
$$$$$$$                    $$$$$$$
$$$$$$$$                  $$$$$$$$
$$$$$$$$$                $$$$$$$$$
$$$$$$$$$$              $$$$$$$$$$
$$$$$$$$$$$            $$$$$$$$$$$
$$$$$$$$$$$$          $$$$$$$$$$$$
$$$$$$$$$$$$$        $$$$$$$$$$$$$
$$$$$$$$$$$$$$      $$$$$$$$$$$$$$
$$$$$$$$$$$$$$$    $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$  $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$  $$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$    $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$      $$$$$$$$$$$$$$
$$$$$$$$$$$$$        $$$$$$$$$$$$$
$$$$$$$$$$$$          $$$$$$$$$$$$
$$$$$$$$$$$            $$$$$$$$$$$
$$$$$$$$$$              $$$$$$$$$$
$$$$$$$$$                $$$$$$$$$
$$$$$$$$                  $$$$$$$$
$$$$$$$                    $$$$$$$
$$$$$$                      $$$$$$
$$$$$                        $$$$$
$$$$                          $$$$
$$$                            $$$
$$                              $$
$                                $