用Java程序打印空心钻石星形图案

194 阅读1分钟

打印空心钻石星图案的Java程序

编写一个Java程序,使用嵌套for循环和if-else语句来打印空心钻石星形图案。这个Java空心钻石的例子检查外侧的钻石线,并打印星星以获得该空心形状。

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondPattern1 { 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 Pattern Rows = ");
	int rows = sc.nextInt();
	
	System.out.println("Printing Hollow Diamond Star Pattern");
	
	for (i = 1 ; i <= rows; i++ ) 
	{
		for (j = 1 ; j <= rows - i; j++ ) 
		{
			System.out.print(" ");	
		}
		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();
	}
	
	for (i = rows - 1 ; i > 0; i-- ) 
	{
		for (j = 1 ; j <= rows - i; j++ ) 
		{
			System.out.print(" ");
		}
		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();
	}
}

在这个Java空心钻石星形图案程序中,我们用一个while循环代替了for循环

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondPattern2 { private static Scanner sc;

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

使用do while循环打印空心钻石星形图案的Java程序

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondPattern3 { private static Scanner sc;

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

在这个Java例子中,HollowDiamondPattern函数打印出一个给定符号的空心钻石图案。

package ShapePrograms;

import java.util.Scanner;

public class HollowDiamondPattern4 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Enter Hollow Diamond Pattern Rows = ");
	int rows = sc.nextInt();
	
	System.out.print("Enter Character for Hollow Diamond Pattern = ");
	char ch = sc.next().charAt(0);
	
	System.out.println("Printing Hollow Diamond Pattern");
	HollowDiamondPattern(rows, ch);
}
public static void HollowDiamondPattern(int rows, char ch) {
	int i, j, k;
	for (i = 1 ; i <= rows; i++ ) 
	{
		for (j = 1 ; j <= rows - i; j++ ) 
		{
			System.out.print(" ");	
		}
		for (k = 1 ; k <= i \* 2 - 1; k++ ) 
		{
			if (k == 1 || k == i \* 2 - 1) {
				System.out.print(ch);
			}
			else {
				System.out.print(" ");
			}
		}
		System.out.println();
	}
	
	for (i = rows - 1 ; i > 0; i-- ) 
	{
		for (j = 1 ; j <= rows - i; j++ ) 
		{
			System.out.print(" ");
		}
		for (k = 1 ; k <= i \* 2 - 1; k++ ) 
		{
			if (k == 1 || k == i \* 2 - 1) {
				System.out.print(ch);
			}
			else {
				System.out.print(" ");
			}
		}
		System.out.println();
	}
}
Enter Hollow Diamond Pattern Rows = 13
Enter Character for Hollow Diamond Pattern = $
Printing Hollow Diamond Pattern
            $
           $ $
          $   $
         $     $
        $       $
       $         $
      $           $
     $             $
    $               $
   $                 $
  $                   $
 $                     $
$                       $
 $                     $
  $                   $
   $                 $
    $               $
     $             $
      $           $
       $         $
        $       $
         $     $
          $   $
           $ $
            $