码迷,mamicode.com
首页 > 编程语言 > 详细

java 空心菱形

时间:2017-05-13 09:52:22      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:技术   http   logs   bsp   nbsp   alt   sys   blog   image   

技术分享

分为两部分,先打印前四行,再打印后三行,

int n = 4;       //设初始值为4
  for(int i=0;i<n;i++) {    //4行
   for(int j=0;j<n-1-i;j++) {    //空格规律:第一行3个,第二行2个,第三行1个,第四行0个;所以循环3次,规律为n-1-i
    System.out.print(" ");
   }
   for(int k=0;k<(2*i+1);k++) {    
    if(k==0||k==2*i) {    //打印前四行的*,中间部分输出空格
     System.out.print("*");
    } else {
     System.out.print(" ");
    }
   }
   System.out.println();
  }
  for(int q=0;q<n-1;q++) {    //输出后三行的前面的空格
   for(int w=0;w<q+1;w++) {
    System.out.print(" ");
   }
   for(int e=0;e<2*n-3-2*q;e++) {
    if(e==0||e==2*n-4-2*q) {      //输出*,中间部分输出空格
     System.out.print("*");
    } else {
     System.out.print(" ");
    }
   }
   System.out.println();
  }

java 空心菱形

标签:技术   http   logs   bsp   nbsp   alt   sys   blog   image   

原文地址:http://www.cnblogs.com/gonghuixin/p/6848022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!