打印X星图案的Java程序

179 阅读1分钟

编写一个Java程序,使用for循环来打印X星形图案。这个Java X星形例子在嵌套的for循环中使用if条件来迭代行。

package ShapePrograms;

import java.util.Scanner;

public class XPattern1 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);	
	
	System.out.print("Please Enter X Pattern Rows = ");
	int rows = sc.nextInt();	
	
	System.out.println("---- Printing X Pattern of Stars ------");
	
	int k = rows \* 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();
	}
}

image.png

在这个Java X星形图案程序中,我们用while循环代替了for循环

package ShapePrograms;

import java.util.Scanner;

public class XPattern2 { private static Scanner sc;

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

```markdown
Please Enter X Pattern Rows = 7
---- Printing X Pattern of Stars ------
*            * 
 *          *  
  *        *   
   *      *    
    *    *     
     *  *      
      *       
     *  *      
    *    *     
   *      *    
  *        *   
 *          *  
*            * 

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

package ShapePrograms;

import java.util.Scanner;

public class XPattern3 { private static Scanner sc;

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

在这个Java例子中,XPattern函数打印了一个给定符号的X图案。

package ShapePrograms;

import java.util.Scanner;

public class XPattern4 { private static Scanner sc;

public static void main(String\[\] args) {
	sc = new Scanner(System.in);
	
	System.out.print("Please Enter X Pattern Rows = ");
	int rows = sc.nextInt();
	
	System.out.print("Enter Character for X Pattern = ");
	char ch = sc.next().charAt(0);
	
	System.out.println("---- Printing X Pattern ------");
	XPattern(rows, ch);
}

public static void XPattern(int rows, char ch) {
	int k = rows \* 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(ch);
			}
			System.out.print(" ");
		}
		System.out.println();
	}
}
Please Enter X Pattern Rows = 10
Enter Character for X Pattern = #
---- Printing X Pattern ------
#                  # 
 #                #  
  #              #   
   #            #    
    #          #     
     #        #      
      #      #       
       #    #        
        #  #         
         #          
        #  #         
       #    #        
      #      #       
     #        #      
    #          #     
   #            #    
  #              #   
 #                #  
#                  #