Java 星形图案程序

本页面列出了最受欢迎且可用的 Java 星形图案程序,其中包含使用循环和函数的多个示例。

请参考 学习 JavaJava 编程示例 来掌握基础知识和完整的程序列表。对于其他图案,例如字母和数字图案,请参考以下 URL。

Java 星形图案程序

以下是 Java 编程面试中经常问到的星形图案程序。我们提供了创建每个图案的最佳方法,并提供了深入了解它们的超链接。

打印星形字母 A 图案

有关打印字母 A 的信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		
		sc = new Scanner(System.in);	
		
		System.out.print("Enter the Number =  ");
		int rw = sc.nextInt();
		
		for (int i = 0 ; i < rw; i++ ) 
		{
			for (int j = 0 ; j <= rw/2; j++ ) 
			{
				if((i == 0 && j != 0 && j != rw/2) || i == rw / 2 ||
						(j == 0 || j == rw /2 ) && i != 0)
					System.out.print("*");
				else
					System.out.print(" ");
			}
			System.out.println("\n");
		}
	}
}

输出

Enter the Number =  10
 **** 

*    *

*    *

*    *

*    *

******

*    *

*    *

*    *

*    *

打印圣诞树

有关圣诞树的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Chirstmas Tree Width & Height = ");
		int w = sc.nextInt();
		int h = sc.nextInt();

		int sp = w * h;
		int x, i, j, k, n = 1;

		for (x = 1; x <= h; x++) {

			for (i = n; i <= w; i++) {
				for (j = sp; j >= i; j--) {
					System.out.print(" ");
				}
				for (k = 1; k <= i; k++) {
					System.out.print("* ");
				}
				System.out.println();
			}
			n = n + 2;
			w = w + 2;
		}
		for (i = 1; i <= h - 1; i++) {
			for (j = sp - 3; j >= 0; j--) {
				System.out.print(" ");
			}
			for (k = 1; k <= h - 1; k++) {
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}

输出

Enter Chirstmas Tree Width & Height = 5 3
               * 
              * * 
             * * * 
            * * * * 
           * * * * * 
             * * * 
            * * * * 
           * * * * * 
          * * * * * * 
         * * * * * * * 
           * * * * * 
          * * * * * * 
         * * * * * * * 
        * * * * * * * * 
       * * * * * * * * * 
             * * 
             * * 

指数增长星形图案

有关指数增长图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Exponentially Increased Pattern Rows = ");
		int rw = sc.nextInt();
		
		for (int i = 0; i < rw; i++ ) 
		{
			for (int j = 1 ; j <= Math.pow(2, i); j++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Exponentially Increased Pattern Rows = 5
*
**
****
********
****************

Java 打印星形菱形图案程序

有关菱形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, rw;
		
		System.out.print("Enter Diamond Rows = ");
		rw = sc.nextInt();
		
		for (i = 1 ; i <= rw; i++ ) 
		{
			printItems(rw, i);
		}
		
		for (i = rw - 1 ; i > 0; i-- ) 
		{
			printItems(rw, i);
		}
	}
	public static void printItems(int rw, int i) 
	{
		for (int j = 1 ; j <= rw - i; j++ ) 
		{
			System.out.print(" ");
		}
		for (int k = 1 ; k <= i * 2 - 1; k++ ) 
		{
			System.out.print("*");
		}
		System.out.println();
		
	}
}

输出

Enter Diamond Rows = 9
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
 ***************
  *************
   ***********
    *********
     *******
      *****
       ***
        *

半菱形星形图案

有关打印半菱形星形图案程序的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i;

		System.out.print("Enter Rows = ");
		int rw = sc.nextInt();

		for (i = 1; i <= rw; i++) {
			result(rw, i);
		}

		for (i = rw - 1; i > 0; i--) {
			result(rw, i);
		}
	}

	public static void result(int rw, int i) {
		for (int j = 1; j <= i; j++) 
		{
			System.out.print("*");
		}
		System.out.println();
	}
}

输出

Enter Rows = 9
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*

空心菱形星形图案程序

有关空心菱形示例的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i, rw;

		System.out.print("Enter Hollow Diamond Star Pattern rows = ");
		rw = sc.nextInt();

		for (i = 1; i <= rw; i++) {
			result(rw, i);
		}

		for (i = rw - 1; i > 0; i--) {
			result(rw, i);
		}
	}
	public static void result(int rw, int i) {
		for (int j = 1; j <= rw - i; j++) {
			System.out.print(" ");
		}
		for (int k = 1; k <= i * 2 - 1; k++) {
			if (k == 1 || k == i * 2 - 1) {
				System.out.print("*");
			} else {
				System.out.print(" ");
			}
		}
		System.out.println();
	}
}

输出

Enter Hollow Diamond Star Pattern rows = 8
       *
      * *
     *   *
    *     *
   *       *
  *         *
 *           *
*             *
 *           *
  *         *
   *       *
    *     *
     *   *
      * *
       *

正方形内的空心菱形图案

有关打印正方形内空心菱形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i, j, k;

		System.out.print("Enter Rows = ");
		int rw = sc.nextInt();

		for (i = 1; i <= rw; i++) {
			for (j = i; j <= rw; j++) {
				System.out.print("*");
			}
			for (j = 1; j <= 2 * i - 2; j++) {
				System.out.print(" ");
			}
			for (k = i; k <= rw; k++) {
				System.out.print("*");
			}
			System.out.println();
		}

		for (i = 1; i <= rw; i++) {
			for (j = 1; j <= i; j++) {
				System.out.print("*");

			}
			for (k = 2 * i - 2; k < 2 * rw - 2; k++) {
				System.out.print(" ");
			}
			for (k = 1; k <= i; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Diamond Pattern inside a Square Rows = 8
****************
*******  *******
******    ******
*****      *****
****        ****
***          ***
**            **
*              *
*              *
**            **
***          ***
****        ****
*****      *****
******    ******
*******  *******
****************

镜像半菱形星型图案

有关镜像半菱形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i;

		System.out.print("Enter Mirrored Half Diamond Star Pattern Rows = ");
		int rw = sc.nextInt();

		for (i = 1; i <= rw; i++) {
			result(rw, i);
		}

		for (i = rw - 1; i > 0; i--) {
			result(rw, i);
		}
	}

	public static void result(int rw, int i) {
		for (int j = 1; j <= rw - i; j++) {
			System.out.print(" ");
		}
		for (int k = 1; k <= i; k++) {
			System.out.print("*");
		}
		System.out.println();
	}
}

输出

Enter Mirrored Half Diamond Star Pattern Rows = 8
       *
      **
     ***
    ****
   *****
  ******
 *******
********
 *******
  ******
   *****
    ****
     ***
      **
       *

Java 金字塔星形图案程序

有关打印金字塔星形图案的更多信息 >> 点击此处。如果您想要金字塔图案程序的完整列表,请使用 本文

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Rows = ");
		int rw = sc.nextInt();

		for (int i = 1; i <= rw; i++) {
			for (int j = 0; j < rw - i; j++) {
				System.out.print(" ");
			}
			for (int k = 0; k < (i * 2) - 1; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Rows = 10
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************

空心金字塔星型图案

有关打印空心金字塔的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Hollow Pyramid Star Pattern Rows = ");
		int rows = sc.nextInt();
		int i, j, k;

		for (i = 1; i <= rows; i++) {
			for (j = 1; j <= rows - i; j++) 
			{
				System.out.print(" ");
			}
			if (i == 1 || i == rows) 
			{
				for (k = 1; k <= (i * 2) - 1; k++) 
				{
					System.out.print("*");
				}
			} 
			else {
				for (k = 1; k <= (i * 2) - 1; k++) 
				{
					if (k == 1 || k == i * 2 - 1) 
					{
						System.out.print("*");
					} else {
						System.out.print(" ");
					}
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Pyramid Star Pattern Rows = 13
            *
           * *
          *   *
         *     *
        *       *
       *         *
      *           *
     *             *
    *               *
   *                 *
  *                   *
 *                     *
*************************

倒金字塔星形图案

有关打印倒金字塔星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Inverted Pyramid Pattern Rows = ");
		int rows = sc.nextInt();

		for (int i = rows; i >= 1; i--) 
		{
			for (int j = 0; j < rows - i; j++) 
			{
				System.out.print(" ");
			}
			for (int k = 0; k != (2 * i) - 1; k++) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Inverted Pyramid Pattern Rows = 12
***********************
 *********************
  *******************
   *****************
    ***************
     *************
      ***********
       *********
        *******
         *****
          ***
           *

空心倒金字塔星形

有关打印空心倒金字塔星形的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Hollow Inverted Pyramid Rows = ");
		int rows = sc.nextInt();

		for (int i = rows ; i > 0; i-- ) 
		{
			for (int j = 1 ; j <= rows - i; j++ ) 
			{
				System.out.print(" ");
			}
			for (int k = 1 ; k <= (2 * i) - 1; k++ ) 
			{
				if (i == 1 || i == rows || k == 1 || k == (2 * i) - 1)
				{
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}			
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Inverted Pyramid Rows = 9
*****************
 *             *
  *           *
   *         *
    *       *
     *     *
      *   *
       * *
        *

Java 直角三角形星形图案程序

有关打印直角三角形星形图案程序的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);	
		
		System.out.print("Enter Right Angled Triangle Pattern Rows = ");
		int rows = sc.nextInt();
		
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= i; j++ ) 	
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Right Angled Triangle Pattern Rows = 12
*
**
***
****
*****
******
*******
********
*********
**********
***********
************

空心直角三角形星形图案

有关打印空心直角三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

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

输出

Enter Hollow Right Angled Triangle Pattern Rows = 14
*
**
* *
*  *
*   *
*    *
*     *
*      *
*       *
*        *
*         *
*          *
*           *
**************

打印倒直角三角形星形图案

有关打印倒直角三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Inverted Right Triangle Pattern Rows = ");
		int rows = sc.nextInt();
		
		for (int i = rows; i > 0; i-- ) 
		{
			for (int j = 0 ; j < i; j++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Inverted Right Triangle Pattern Rows = 15
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*

空心倒直角三角形星形图案

有关打印空心倒直角三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Hollow Inverted Right Triangle Pattern Rows = ");
		int rows = sc.nextInt();

		for (int i = rows; i > 0; i--) 
		{
			if (i == 1 || i == rows) 
			{
				for (int j = 1; j <= i; j++) 
				{
					System.out.print("*");
				}
			} 
			else {
				for (int k = 1; k <= i; k++) 
				{
					if (k == 1 || k == i) {
						System.out.print("*");
					} else {
						System.out.print(" ");
					}
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Inverted Right Triangle Pattern Rows = 13
*************
*          *
*         *
*        *
*       *
*      *
*     *
*    *
*   *
*  *
* *
**
*

倒镜像直角三角形星形图案

有关打印倒镜像直角三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Inverted Mirrored Right Triangle Pattern Rows = ");
		int rows = sc.nextInt();
		
		for (int i = rows; i > 0; i-- ) 
		{
			for (int j = rows - i ; j > 0; j-- ) 
			{
				System.out.print(" ");
			}
			for (int k = 0 ; k < i; k++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Inverted Mirrored Right Triangle Pattern Rows = 12
************
 ***********
  **********
   *********
    ********
     *******
      ******
       *****
        ****
         ***
          **
           *

镜像直角三角形星形图案

有关打印镜像直角三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

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

输出

Enter Mirrored Right Triangle Pattern Rows = 9
        *
       **
      ***
     ****
    *****
   ******
  *******
 ********
*********

反向镜像直角三角形星形图案

有关打印反向镜像直角三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	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();

		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();
		}
	}
}

输出

Enter Reverse Mirrored Right Triangle Rows = 14
**************
 *************
  ************
   ***********
    **********
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *

Java 三角形星形图案程序

有关打印三角形星形图案的更多信息 >> 点击此处。

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Triangle Rows = ");
		int rows = sc.nextInt();

		for (int i = 1 ; i <= rows; i++ )
		{
			for (int j = 1 ; j <= i; j++ ) 
			{
				System.out.print("* ");
			}
			System.out.println("");
		}
	}
}

输出

Enter Triangle Rows = 10
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * 
* * * * * * * 
* * * * * * * * 
* * * * * * * * * 
* * * * * * * * * * 

向下三角形

有关打印向下三角形的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Downward Triangle Rows = ");
		int rw = sc.nextInt();
		
		
		for (int i = rw -1 ; i >= 0; i-- ) 
		{
			for (int j = 0 ; j <= i; j++ ) 
			{
				System.out.print("* ");
			}
			System.out.println();
		}
	}
}

输出

Enter Downward Triangle Rows = 13
* * * * * * * * * * * * * 
* * * * * * * * * * * * 
* * * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * 
* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

打印左帕斯卡三角形星形

有关左帕斯卡三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i;

		System.out.print("Enter Left Pascals Triangle Pattern Rows = ");
		int rows = sc.nextInt();

		for (i = 1; i <= rows; i++) {
			result(rows, i);
		}
		for (i = rows - 1; i >= 1; i--) {
			result(rows, i);
		}
	}
	public static void result(int rows, int i) {
		for (int j = i; j < rows; j++) 
		{
			System.out.print("  ");
		}
		for (int k = 1; k <= i; k++) 
		{
			System.out.print("* ");
		}
		System.out.println();

	}
}

输出

Enter Left Pascals Triangle Pattern Rows = 8
              * 
            * * 
          * * * 
        * * * * 
      * * * * * 
    * * * * * * 
  * * * * * * * 
* * * * * * * * 
  * * * * * * * 
    * * * * * * 
      * * * * * 
        * * * * 
          * * * 
            * * 
              * 

空心左帕斯卡三角形星形

有关空心左帕斯卡三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i, j, k;

		System.out.print("Enter Hollow Left Pascals Triangle Rows = ");
		int rows = sc.nextInt();

		for (i = 1; i <= rows; i++) {
			for (j = rows; j > i; j--) {
				System.out.print(" ");
			}
			for (k = 1; k <= i; k++) {
				if (k == 1 || k == i) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		for (i = 1; i <= rows - 1; i++) {
			for (j = 1; j <= i; j++) {
				System.out.print(" ");
			}
			for (k = rows - 1; k >= i; k--) {
				if (k == rows - 1 || k == i) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Left Pascals Triangle Rows = 10
         *
        **
       * *
      *  *
     *   *
    *    *
   *     *
  *      *
 *       *
*        *
 *       *
  *      *
   *     *
    *    *
     *   *
      *  *
       * *
        **
         *

右帕斯卡三角形星形

有关右帕斯卡三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i;

		System.out.print("Enter Right Pascals Triangle Pattern Rows = ");
		int rows = sc.nextInt();

		for (i = 0; i < rows; i++) {
			result(rows, i);
		}
		for (i = rows; i >= 0; i--) {
			result(rows, i);
		}
	}
	public static void result(int rows, int i) {
		for (int j = 0; j < i; j++) 
		{
			System.out.print("* ");
		}
		System.out.println();

	}
}

输出

Enter Right Pascals Triangle Pattern Rows = 9

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

空心右帕斯卡三角形星形

有关空心右帕斯卡三角形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i, j, k, rows;

		System.out.print("Enter Hollow Right Pascals Triangle Rows = ");
		rows = sc.nextInt();

		for (i = 1; i <= rows; i++) {
			for (j = 1; j <= i; j++) {
				if (j == 1 || j == i) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		for (i = 1; i <= rows - 1; i++) {
			for (j = rows - 1; j >= i; j--) {
				if (j == rows - 1 || j == i || i == rows) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			for (k = 1; k < i; k++) {
				System.out.print(" ");
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Right Pascals Triangle Rows = 8
*
**
* *
*  *
*   *
*    *
*     *
*      *
*     *
*    * 
*   *  
*  *   
* *    
**     
*  

Java 方形星形图案程序

有关打印方形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter any Side of a Square : ");
		int side = sc.nextInt();	
			
		for(int i = 1; i <= side; i++)
		{
			for(int j = 1; j <= side; j++)
			{
				System.out.print("*"); 
			}
			System.out.print("\n"); 
		}	
	}
}

输出

Please Enter any Side of a Square : 12
************
************
************
************
************
************
************
************
************
************
************
************

空心方形星形图案

有关空心方形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter any Side of a Square : ");
		int side = sc.nextInt();	
			
		for(int i = 0; i < side; i++)
		{
			for(int j = 0; j < side; j++)
			{
				if (i == 0 || i == side - 1 || j == 0 || j == side - 1) 
				{
					System.out.print("* ");
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.print("\n"); 
		}	
	}
}

输出

Please Enter any Side of a Square : 14
* * * * * * * * * * * * * * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
*                         * 
* * * * * * * * * * * * * * 

带对角线的空心方形

有关带对角线的空心方形的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Hollow Square with Diagonals side = ");
		int s = sc.nextInt();

		for (int i = 1; i <= s; i++) 
		{
			for (int j = 1; j <= s; j++) 
			{
				if (i == 1 || i == s || i == j || j == 1 || j == s || j == s - i + 1) {
					System.out.print("* ");
				} 
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Square with Diagonals side = 10
* * * * * * * * * * 
* *             * * 
*   *         *   * 
*     *     *     * 
*       * *       * 
*       * *       * 
*     *     *     * 
*   *         *   * 
* *             * * 
* * * * * * * * * * 

Java 菱形星形图案程序

有关打印菱形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter Rhombus Pattern Rows = ");
		int rows = sc.nextInt();
		
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= rows - i; j++ ) 
			{
				System.out.print(" ");
			}
			for (int k = 1 ; k <= rows; k++ ) 
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Please Enter Rhombus Pattern Rows = 9
        *********
       *********
      *********
     *********
    *********
   *********
  *********
 *********
*********

空心菱形星形图案

有关空心菱形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Rhombus Pattern Rows = ");
		int rows = sc.nextInt();
		
		for (int i = 1 ; i <= rows; i++ ) 
		{
			for (int j = 1 ; j <= rows - i; j++ ) 
			{
				System.out.print(" ");
			}
			for (int k = 1 ; k <= rows; k++ ) 
			{
				if (i == 1 || i == rows || k == 1 || k == rows) {
					System.out.print("* ");
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Rhombus Pattern Rows = 12
           * * * * * * * * * * * * 
          *                     * 
         *                     * 
        *                     * 
       *                     * 
      *                     * 
     *                     * 
    *                     * 
   *                     * 
  *                     * 
 *                     * 
* * * * * * * * * * * * 

镜像菱形星形图案

有关打印镜像菱形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

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

输出

Enter Mirrored Rhombus Pattern Rows = 13
*************
 *************
  *************
   *************
    *************
     *************
      *************
       *************
        *************
         *************
          *************
           *************
            *************

空心镜像菱形星型图案

有关打印空心镜像菱形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = ");
		int rows = sc.nextInt();

		for (int i = 1; i <= rows; i++) 
		{
			for (int j = i; j > 0; j--) {
				System.out.print(" ");
			}
			if (i == 1 || i == rows) {
				for (int k = 1; k <= rows; k++) {
					System.out.print("* ");
				}
			} 
			else {
				for (int k = 1; k <= rows; k++) {
					if (k == 1 || k == rows) {
						System.out.print("* ");
					} else {
						System.out.print("  ");
					}
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Mirrored Rhombus Pattern Rows = 12
 * * * * * * * * * * * * 
  *                     * 
   *                     * 
    *                     * 
     *                     * 
      *                     * 
       *                     * 
        *                     * 
         *                     * 
          *                     * 
           *                     * 
            * * * * * * * * * * * * 

Java 打印矩形星形图案程序

有关打印星形矩形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		int rows, columns;
		sc = new Scanner(System.in);
		
		System.out.print("Enter Number of Rows : ");
		rows = sc.nextInt();	
		
		System.out.print("Enter Number of Columns : ");
		columns = sc.nextInt();		
			
		for(int i = 1; i <= rows; i++)
		{
			for(int j = 1; j <= columns; j++)
			{
				System.out.print("* "); 
			}
			System.out.print("\n"); 
		}	
	}
}

输出

Enter Number of Rows : 10
Enter Number of Columns : 15
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 

空心矩形星型图案

有关打印空心矩形星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Rectangle Rows & Columns = ");
		int rows = sc.nextInt();
		int columns = sc.nextInt();
				
		for (int i = 0; i < rows; i++ ) 
		{
			for (int j = 0 ; j < columns; j++ ) 
			{
				if (i == 0 || i == rows - 1 || j == 0 || j == columns - 1) {
					System.out.print("* ");
				}
				else {
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}

输出

Enter Hollow Rectangle Rows & Columns = 9 17
* * * * * * * * * * * * * * * * * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
*                               * 
* * * * * * * * * * * * * * * * * 

Java 打印沙漏星形图案程序

有关打印星形沙漏图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, rows;
		
		System.out.print("Enter Sandglass Pattern Rows = ");
		rows = sc.nextInt();
				
		for (i = 0 ; i <= rows - 1; i++ ) 
		{
			result(rows, i);
		}
		
		for (i = rows - 1; i >= 0; i-- ) 
		{
			result(rows, i);
		}
	}
	public static void result(int rows, int i) {
		for (int j = 0 ; j < i; j++ ) 
		{
			System.out.print(" ");
		}
		for(int k = i; k <= rows - 1; k++) 
		{
			System.out.print("* ");
		}
		System.out.println();
	}
}

输出

Java Star Pattern Programs

空心沙漏星形图案

有关打印空心沙漏星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, rows;
		
		System.out.print("Enter Hollow Sandglass Pattern Rows = ");
		rows = sc.nextInt();
				
		for (i = 1 ; i <= rows; i++ ) 
		{
			result(rows, i);
		}
		
		for (i = rows - 1; i >= 1; i-- ) 
		{
			result(rows, i);
		}
	}
	public static void result(int rows, int i) {
		for (int j = 1 ; j < i; j++ ) 
		{
			System.out.print(" ");
		}
		for(int k = i; k <= rows; k++) 
		{
			if(i == 1 || k == i || k == rows)
			{
				System.out.print("* ");
			}
			else 
			{
				System.out.print("  ");
			}
		}
		System.out.println();
	}
}

输出

Enter Hollow Sandglass Pattern Rows = 10
* * * * * * * * * * 
 *               * 
  *             * 
   *           * 
    *         * 
     *       * 
      *     * 
       *   * 
        * * 
         * 
        * * 
       *   * 
      *     * 
     *       * 
    *         * 
   *           * 
  *             * 
 *               * 
* * * * * * * * * * 

剩余的 Java 星形图案程序

以下示例打印了不同形状的星形图案。

左箭头星型图案

有关打印左箭头星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		int i, j;

		System.out.print("Enter Left Arrow Pattern Rows = ");
		int rows = sc.nextInt();

		for (i = 1; i <= rows; i++) {
			for (j = 1; j <= rows - i; j++) {
				System.out.print(" ");
			}
			for (j = i; j <= rows; j++) {
				System.out.print("*");
			}
			System.out.println();
		}

		for (i = 1; i < rows; i++) {
			for (j = 0; j < i; j++) {
				System.out.print(" ");
			}
			for (j = 0; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Enter Left Arrow Pattern Rows = 9
        *********
       ********
      *******
     ******
    *****
   ****
  ***
 **
*
 **
  ***
   ****
    *****
     ******
      *******
       ********
        *********

右箭头星型图案

有关打印右箭头星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	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();

		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();
		}
	}
}

输出

Please Enter Right Arrow Pattern Rows = 8
********
 *******
  ******
   *****
    ****
     ***
      **
       *
      **
     ***
    ****
   *****
  ******
 *******
********

加号 (+) 星形图案

有关打印加号星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	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();
				
		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();
		}
	}
}

输出

Please Enter Plus Pattern Rows = 7
      * 
      * 
      * 
      * 
      * 
      * 
*************
      * 
      * 
      * 
      * 
      * 
      * 

Java 打印 V 星形图案程序

有关打印 V 星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter V Shape Star Pattern Rows = ");
		int rw = sc.nextInt();
		
		
		for (int i = 1; i <= rw; i++ ) 
		{
			printItems(rw, i);
			for (int k = 1 ; k <= 2 * (rw - i); k++ ) 
			{
				System.out.print(" ");
			}
			printItems(rw, i);
			System.out.println();
		}
	}
	public static void printItems(int rw, int i) {
		for (int j = 1 ; j <= i; j++ ) 
		{
			System.out.print("*");
		}
		
	}
}

输出

Enter V Shape Star Pattern Rows = 12
*                      *
**                    **
***                  ***
****                ****
*****              *****
******            ******
*******          *******
********        ********
*********      *********
**********    **********
***********  ***********
************************

倒 V 星型图案

有关打印倒 V 星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Inverted V Shape Star Pattern Rows = ");
		int rw = sc.nextInt();
		
		
		for (int i = rw; i >= 1; i-- ) 
		{
			printItems(rw, i);
			for (int k = 1 ; k <= 2 * (rw - i); k++ ) 
			{
				System.out.print(" ");
			}
			printItems(rw, i);
			System.out.println();
		}
	}
	public static void printItems(int rw, int i) {
		for (int j = 1 ; j <= i; j++ ) 
		{
			System.out.print("*");
		}
		
	}
}

输出

Enter Inverted V Shape Star Pattern Rows = 10
********************
*********  *********
********    ********
*******      *******
******        ******
*****          *****
****            ****
***              ***
**                **
*                  *

H 星型图案

有关打印 H 星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	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 rw = sc.nextInt();

		for (i = 1; i <= rw; i++) {
			for (j = 1; j <= i; j++) {
				System.out.print("*");
			}
			for (k = i * 2; k <= rw * 2 - 1; k++) {
				System.out.print(" ");
			}
			for (l = 1; l <= i; l++) {
				System.out.print("*");
			}
			System.out.println();
		}

		for (i = 1; i <= rw - 1; i++) {
			for (j = rw - 1; j >= i; j--) {
				System.out.print("*");
			}
			for (k = 1; k <= i * 2; k++) {
				System.out.print(" ");
			}
			for (l = rw - 1; l >= i; l--) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

输出

Please Enter H Pattern Rows = 8
*              *
**            **
***          ***
****        ****
*****      *****
******    ******
*******  *******
****************
*******  *******
******    ******
*****      *****
****        ****
***          ***
**            **
*              *

W 星型图案

有关打印 W 星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter W Shape Star Pattern Rows = ");
		int rw = sc.nextInt();
			
		for(int i = 0; i < rw; i++)
		{
			printStars(i + 1);
			printSpaces(rw - i - 1);
			printStars(rw - i + 1);
			printSpaces(2 * i);
			printStars(rw - i);
			printSpaces(rw - i - 1);
			printStars(i + 1);
			System.out.println();
		}
	}
	public static void printStars(int rw) 
	{
		for (int i = 0; i < rw; ++i) 
		{
			System.out.print("*");
		}
	}
	public static void printSpaces(int rw) 
	{
		for (int i = 0; i < rw; ++i ) 
		{
			System.out.print(" ");
		}
	}
}

输出

Enter W Shape Star Pattern Rows = 7
*      ***************      *
**     *******  ******     **
***    ******    *****    ***
****   *****      ****   ****
*****  ****        ***  *****
****** ***          ** ******
*********            ********

X 星型图案

有关打印 X 星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);	
		
		System.out.print("Please Enter X Pattern Rows = ");
		int rw = sc.nextInt();	
		
		int k = rw * 2 - 1;	
		
		for (int i = 1 ; i <= k; i++ ) 
		{
			for (int j = 1 ; j <= k; j++ ) 
			{
				if(j == i || j == k - i + 1) 
				{
					System.out.print("*");
				}
				System.out.print(" ");
			}
			System.out.println();
		}
	}
}

输出

Please Enter X Pattern Rows = 8
*              * 
 *            *  
  *          *   
   *        *    
    *      *     
     *    *      
      *  *       
       *        
      *  *       
     *    *      
    *      *     
   *        *    
  *          *   
 *            *  
*              * 

Java 打印 8 字星形图案程序

有关打印 8 字星形图案的更多信息 >> 点击此处

import java.util.Scanner;

public class Example {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);

		System.out.print("Please Enter 8 Pattern Rows = ");
		int rw = sc.nextInt();

		for (int i = 1; i <= rw * 2 - 1; i++) 
		{
			if (i == 1 || i == rw || i == rw * 2 - 1) 
			{
				for (int j = 1; j <= rw; j++) 
				{
					if (j == 1 || j == rw) {
						System.out.print(" ");
					} 
					else {
						System.out.print("*");
					}
				}
			} 
			else 
			{
				for (int k = 1; k <= rw; k++) 
				{
					if (k == 1 || k == rw) {
						System.out.print("*");
					} 
					else {
						System.out.print(" ");
					}
				}
			}
			System.out.println();
		}
	}
}

输出

Please Enter 8 Pattern Rows = 6
 **** 
*    *
*    *
*    *
*    *
 **** 
*    *
*    *
*    *
*    *
 ****