A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© tsiov 初级黑马   /  2015-7-9 20:51  /  521 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

不多说,希望能积累技术分,分享近期自己写的一些代码。
  1. package test;

  2. /*******************************
  3. (3)请用循环
  4. 输出如下结果:
  5.      *                    *
  6.     ***                   * *
  7.    *****          *   *
  8.   *******         *     *
  9. *********        *********
  10. *******************************/
  11. class Draw{
  12.         final int HowBig=4;                //Decides how big of the pyramid.
  13.         int st=1;
  14.         void PaintStar(){
  15.                 System.out.print("*");
  16.         }
  17.         void PaintSpace(){
  18.                 System.out.print(" ");
  19.         }
  20.         void NextLine(){
  21.                 System.out.println();
  22.         }
  23.         void FullStars(){
  24.                 st=1;                //init
  25.                 for(int i=HowBig;i>=0;i--){
  26.                         for(int sc=0;sc<i;sc++){
  27.                                 PaintSpace();
  28.                         }
  29.                         for(int sa=0;sa<st;sa++){
  30.                                 PaintStar();
  31.                         }
  32.                         st+=2;
  33.                         NextLine();
  34.                 }
  35.         }
  36.         void EmptyStars(){
  37.                 st=1;                //init head
  38.                 for(int i=HowBig;i>=0;i--){
  39.                         for(int sc=0;sc<i;sc++){
  40.                                 PaintSpace();
  41.                         }
  42.                         for(int sa=0;sa<st;sa++){
  43.                                 if(sa==0||sa==st-1||i==0){
  44.                                         PaintStar();
  45.                                 }
  46.                                 else{
  47.                                         PaintSpace();
  48.                                 }
  49.                         }
  50.                         st+=2;
  51.                         NextLine();
  52.                 }
  53.         }
  54. }
  55. public class Pyramid {
  56.         public static void main(String ARGS[]){
  57.                 Draw dr=new Draw();
  58.                 dr.FullStars();                //实心金字塔
  59.                 dr.EmptyStars();        //空心金字塔
  60.         }
  61. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马