当前位置: 代码迷 >> Java相关 >> 高手勿喷:菱形打印就改进
  详细解决方案

高手勿喷:菱形打印就改进

热度:282   发布时间:2011-09-23 13:52:54.0
高手勿喷:菱形打印就改进
程序代码:
/*
    作者:Heart
    时间:2011年9月23日13:15:00
    功能:①    *
               * *
              *   *
             *     *
            *       *
             *     *
              *   *
               * *
                *

*/



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

    }
}

















[ 本帖最后由 Heart→M鹏 于 2011-9-23 13:54 编辑 ]
----------------解决方案--------------------------------------------------------
程序代码:
public class PyramidText
{
     public static void main(String []args)
     {
         for(int y = -5; y <= 5; ++y)
         {
             for(int x = -5; x <= 5; ++x)
             {
                 if(Math.abs(x+y) == 5||Math.abs(x-y) == 5) //也可以Math.abs(x)+Math.abs(y) == 5
                     System.out.print('*');
                 else
                     System.out.print(' ');            
             }
             System.out.println();
         }
    }
}


//从美琴姐那里借荐而来的。



[ 本帖最后由 czsbc 于 2011-9-26 00:30 编辑 ]
----------------解决方案--------------------------------------------------------
谢谢叻哈
----------------解决方案--------------------------------------------------------
好像c++的程序了
----------------解决方案--------------------------------------------------------
第二个中if(Math.abs(x+y) == 5||Math.abs(x-y) == 5),什么意思?

----------------解决方案--------------------------------------------------------
  相关解决方案