打印棱形--进阶(Java)

第一题

打印如下棱形,大小符号从控制台输入

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

分析:以棱形的中心点为中心建立二维坐标系,发现棱形上的所有的点横纵坐标的绝对值之和都小于某个值。反之只要满足横纵坐标绝对值之和小于这个值,那这个点就一定在棱形上。

//		主函数
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("row:");
        int row=sc.nextInt();
        System.out.println("symbol:");
        String symbol=sc.next();

        String[][] array=new String[row*2-1][row*2-1];

//        初始化数组
        for (int i=0;i<(2*row-1);i++){
            for (int j=0;j<(2*row-1);j++){
                array[i][j]=" ";
            }
        }

//		将在棱形上的点(数组元素)重新赋值
		for (int i=0;i<(2*row-1);i++){
            for (int j=0;j<(2*row-1);j++){
                if (isPrint(i,j,row-1,row)){
                    array[i][j]=symbol;
                }
            }
        }
        
//		打印数组
		printArrays(array);
		
}
//    判断是否打印
    public static boolean isPrint(int i,int j,int focus,int len){
        if ((Math.abs(focus-i)+Math.abs(focus-j))<len){
            return true;
        }
        return false;
    }

//    打印最终结果
    public static void printArrays(String[][] array){
        for (String[] arr:array){
            for (String i:arr){
                System.out.print(i);
            }
            System.out.println();
        }
    }

结果:
图1


第二题

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

在问题一的基础上,打印小一点的棱形覆盖上去就行了

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("row:");
        int row=sc.nextInt();
        System.out.println("symbol:");
        String symbol=sc.next();

        String[][] array=new String[row*2-1][row*2-1];

//        初始化数组
        for (int i=0;i<(2*row-1);i++){
            for (int j=0;j<(2*row-1);j++){
                array[i][j]=" ";
            }
        }
//		将在棱形上的点(数组元素)重新赋值为  *
        for (int i=0;i<(2*row-1);i++){
            for (int j=0;j<(2*row-1);j++){
                if (isPrint(i,j,row-1,row)){
                    array[i][j]=symbol;
                }
            }
        }
//		将在小号棱形上的点(数组元素)重新赋值为空格
        for (int i=0;i<(2*row-1);i++){
            for (int j=0;j<(2*row-1);j++){
                if (isPrint(i,j,row-1,row-1)){
                    array[i][j]=" ";
                }
            }
        }

        printArrays(array);
   }     

结果:
图二


第三题

	   *       
      * *      
     *   *     
    *  *  *    
   *  * *  *   
  *  *   *  *  
 *  *  *  *  * 
*  *  * *  *  *
 *  *  *  *  * 
  *  *   *  *  
   *  * *  *   
    *  *  *    
     *   *     
      * *      
       *  
import java.util.Scanner;

public class demo {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("row:");
        int row=sc.nextInt();
        System.out.println("symbol:");
        String symbol=sc.next();

        String[][] array=new String[row*2-1][row*2-1];

//        初始化数组
        for (int i=0;i<(2*row-1);i++){
            for (int j=0;j<(2*row-1);j++){
                array[i][j]=" ";
            }
        }
        


        overPrint(row,array,symbol,0);
        printArrays(array);

    }

//  打印覆盖
    public static String[][] overPrint(int row,String[][] array,String symbol,int count){

        for (int i=0;i<(2*row-1);i++){
            for (int j=0;j<(2*row-1);j++){
                if (isPrint(i,j,row-1,row-count)){
                    if (count%2==0){
                        array[i][j]=symbol;
                    }else{
                        array[i][j]="-";
                    }
                }
            }
        }
        count++;
        if (row!=count){
            overPrint(row,array,symbol,count);
        }
       return array;

    }



//    判断是否打印
    public static boolean isPrint(int i,int j,int focus,int len){
        if ((Math.abs(focus-i)+Math.abs(focus-j))<len){
            return true;
        }
        return false;
    }


//    打印最终结果
    public static void printArrays(String[][] array){
        for (String[] arr:array){
            for (String i:arr){
                System.out.print(i);
            }
            System.out.println();
        }
    }

}

结果:
这里为了便于观看结果,我将棱形中间的花纹设置成 -

图三


问题四

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值