用Java程序在一个正方形内打印空心钻石图案

324 阅读2分钟

编写一个Java程序,使用while循环在方形星形图案内打印空心钻石图案。在这里,我们使用多个while循环和if-else语句来打印方形图案内的空心钻石图案。

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondinsideSquare1 {
private static Scanner sc;

public static void main(String[] args) {
	sc = new Scanner(System.in);
	
	int i, j, k;
	
	System.out.print("Enter Hollow Diamond inside a Square Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Printing Hollow Diamond Star Pattern inside a Square");
	i = 0 ;
	while( i < rows) 
	{
		j = 0 ;
		while ( j < rows  ) 
		{
			if(j < rows - i) {
				System.out.print("*");
			}
			else {
				System.out.print(" ");
			}
			j++;
		}
		k = 0 ;
		while ( k < rows) 
		{
			if (k < i ) {
				System.out.print(" ");
			}
			else {
				System.out.print("*");
			}
			k++ ;
		}
		System.out.println();
		i++;
	}
	
	i = 1 ;
	while ( i <= rows ) 
	{
		j = 0 ;
		while ( j < rows ) 
		{
			if(j < i) {
				System.out.print("*");
			}
			else {
				System.out.print(" ");
			}
			j++;
		}
		k = 0 ;
		while ( k < rows) 
		{
			if (k < rows - i ) {
				System.out.print(" ");
			}
			else {
				System.out.print("*");
			}
			k++ ;
		}
		System.out.println();
		i++;
	}
}
Enter Hollow Diamond inside a Square Rows = 8
Printing Hollow Diamond Star Pattern inside a Square
****************
*******  *******
******    ******
*****      *****
****        ****
***          ***
**            **
*              *
*              *
**            **
***          ***
****        ****
*****      *****
******    ******
*******  *******
****************

在这个Java空心钻石星形图案的正方形程序中,我们改变了上述代码,删除了所有的if-else语句。

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondinsideSquare2 {
private static Scanner sc;

public static void main(String[] args) {
	sc = new Scanner(System.in);
	
	int i, j, k;
	
	System.out.print("Enter Hollow Diamond inside a Square Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Printing Hollow Diamond Star Pattern inside a Square");
	
	for (i = 1 ; i <= rows; i++ ) 
	{
		for (j = i ; j <= rows; j++ ) 
		{
			System.out.print("*");
		}
		for (j = 1 ; j <= 2 * i - 2; j++ ) 
		{
			System.out.print(" ");
		}
		for (k = i ; k <= rows; k++ ) 
		{
			System.out.print("*");
		}
		System.out.println();
	}
	
	for (i = 1 ; i <= rows; i++ ) 
	{
		for (j = 1 ; j <= i; j++ ) 
		{
			System.out.print("*");

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

}

这个Java方形空心钻石图案的程序与第一个例子相同,我们用for循环代替了while循环

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondinsideSquare3 {
private static Scanner sc;

public static void main(String[] args) {
	sc = new Scanner(System.in);
	
	int i, j, k;
	
	System.out.print("Enter Hollow Diamond inside a Square Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Printing Hollow Diamond Star Pattern inside a Square");
	
	for (i = 0 ; i < rows; i++ ) 
	{
		for (j = 0 ; j < rows; j++ ) 
		{
			if(j < rows - i) {
				System.out.print("*");
			}
			else {
				System.out.print(" ");
			}
		}
		for (k = 0 ; k < rows; k++ ) 
		{
			if (k < i ) {
				System.out.print(" ");
			}
			else {
				System.out.print("*");
			}
		}
		System.out.println();
	}
	
	for (i = 1 ; i <= rows; i++ ) 
	{
		for (j = 0 ; j < rows; j++ ) 
		{
			if(j < i) {
				System.out.print("*");
			}
			else {
				System.out.print(" ");
			}
		}
		for (k = 0 ; k < rows; k++ ) 
		{
			if (k < rows - i ) {
				System.out.print(" ");
			}
			else {
				System.out.print("*");
			}
		}
		System.out.println();
	}
}
Enter Hollow Diamond inside a Square Rows = 12
Printing Hollow Diamond Star Pattern inside a Square
************************
***********  ***********
**********    **********
*********      *********
********        ********
*******          *******
******            ******
*****              *****
****                ****
***                  ***
**                    **
*                      *
*                      *
**                    **
***                  ***
****                ****
*****              *****
******            ******
*******          *******
********        ********
*********      *********
**********    **********
***********  ***********
************************