Java 打印菱形星形图案程序

使用 for 循环编写一个 Java 程序来打印菱形星形图案。这个菱形星形示例使用多个 for 循环嵌套在 for 循环中来迭代菱形行。

package ShapePrograms;

import java.util.Scanner;

public class RhombusPattern1 {
	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();
		
		System.out.println("-- Printing Rhombus Pattern of Stars ---");
		
		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();
		}
	}
}
Java Program to Print Rhombus Star Pattern

在这个菱形星形 程序 中,我们用 while 循环 替换了嵌套的 for 循环

package ShapePrograms;

import java.util.Scanner;

public class RhombusPattern2 {
	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();
		
		System.out.println("--- Printing Rhombus Pattern of Stars ---");
		int i = 1 ;
		while( i <= rows ) 
		{
			int j = 1;
			while(j <= rows - i) 
			{
				System.out.print(" ");
				j++;
			}
			int k = 1;
			while(k <= rows) 
			{
				System.out.print("*");
				k++;
			}
			System.out.println();
			i++;
		}
	}
}
Please Enter Rhombus Pattern Rows = 12
--- Printing Rhombus Pattern of Stars ---
           ************
          ************
         ************
        ************
       ************
      ************
     ************
    ************
   ************
  ************
 ************
************

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

package ShapePrograms;

import java.util.Scanner;

public class RhombusPattern3 {
	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();
		
		System.out.println("-- Printing Rhombus Pattern of Stars ---");
		int i = 1 ;
		do
		{
			int j = 1;
			do 
			{
				System.out.print(" ");
			}while(j++ <= rows - i);
			int k = 1;
			do
			{
				System.out.print("*");
			}while(++k <= rows);
			System.out.println();
			i++;
		}while(i <= rows);
	}
}
Please Enter Rhombus Pattern Rows = 10
-- Printing Rhombus Pattern of Stars ---
          **********
         **********
        **********
       **********
      **********
     **********
    **********
   **********
  **********
 **********

在此示例中,RhombusPattern 函数打印用户给定符号的菱形图案。

package ShapePrograms;

import java.util.Scanner;

public class RhombusPattern4 {
private static Scanner sc;

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

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

System.out.print("Please Enter Character for Rhombus Pattern = ");
char ch = sc.next().charAt(0);

System.out.println("---- Printing Rhombus Pattern ------");
RhombusPattern(rows, ch);

}
public static void RhombusPattern(int rows, char ch) {
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(ch);
}
System.out.println();
}
}
}
Please Enter Rows = 15
Please Enter Character for Rhombus Pattern = #
---- Printing Rhombus Pattern ------
              ###############
             ###############
            ###############
           ###############
          ###############
         ###############
        ###############
       ###############
      ###############
     ###############
    ###############
   ###############
  ###############
 ###############
###############