hc学习平台

 找回密码
 立即注册
搜索
热搜: 活动 交友 javakc
 › 糖豆 › 日志

通过for语句,使用*打印输出菱形

2022-08-05 17:20:44 查看(41) 回复(0)

    *    

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

    *  

版本1:实现输出空心菱形

版本2:实现输出实心菱形

版本3:输入偶数数字,根据数字输出菱形

public class xing {
    public static void main(String[] args) {
        int layer = 5;
        for(int i=1;i<=layer;i++){
            int spaceNum = layer-i;
            for(int j=1;j<=spaceNum;j++){
                System.out.print(" ");
                }
            int starNum = 2*i-1;
            for(int j=1;j<=starNum;j++){
                if(j==1||j==starNum){
                    System.out.print("*");
                    }else{
                    System.out.print(" ");
                    }
                }
            System.out.println();
            }
        for(int i=(layer-1);i>=1;i--){
            int sapceNum = layer-i;
            for(int j=1;j<=sapceNum;j++){
                System.out.print(" ");
                }
            int starNum = 2*i-1;
            for(int j=1;j<=starNum;j++){
                if(j==1||j==starNum){
                    System.out.print("*");
                    }else{
                    System.out.print(" ");
                    }
                }
            System.out.println();
        }
    }
}

评论 (0 个评论)

facelist

全部作者的其他最新日志



站点统计|举报|Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, , Processed in 0.195171 second(s), 9 queries .

Powered by javakc! X1.0

© 2004-2014 javakc

f1208.com 备案号:京ICP备14030918号-1

返回顶部