用java編寫小程序打印“金字塔”


package demo1;


public class Demo {

    public static void main(String[] args) 
    {
        int a=4;
        for(int i=1;i<=a;i++)
        {
            for(int k=1;k<=a-i;k++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=(i-1)*2+1;j++)
            {
                System.out.print("*");
            }
            System.out.println();
        }
        

    }

}

     *
   ***

  *****

*******

package demo1;


public class Demo {

    public static void main(String[] args) 
    {
        int a=4;
        for(int i=1;i<=a;i++)
        {
            for(int k=1;k<=a-i;k++)
            {
                System.out.print(" ");
            }
            for(int j=1;j<=(i-1)*2+1;j++)
            {
                if(i==1||i==a)
                {
                System.out.print("*");
                }
                else
                {
                    if( j==1||j==(i-1)*2+1)
                    {
                        System.out.print("*");
                    }
                    else
                    {
                        System.out.print(" ");
                    }
                        
                }
            }
            System.out.println();
        }
        

    }

}
View Code

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM