日期:2014-05-20  浏览次数:20797 次

求助!各位大神,请帮忙优化下
本人是新学JAVA SE的新手,才学习三天
我自己写了一个有控制面板画出三角形,
代码如下:
class day03
{
public static void main(String[] args) 
{
for (int a=0;a<=6 ;a++ )
{
for (int b=a; b<=5;b++ )
{
System.out.print(" ");
}
for (int c=0;c<=a ;c++ )
{
System.out.print("*");
}
for (int d=1;d<=a ;d++ )
{
System.out.print("*");
}
for (int e=a;e<=5;e++ )
{
System.out.print(" ");
}
System.out.println();
}
}
}

我想请各位大神帮忙优化下,我觉得for用得太多了,有其他办法完成没有,谢谢各位。

------解决方案--------------------
随手写了一个,希望对你有帮助
Java code

    public static void main(String[] args)  
    {
        int col = 6 ;
        for( int i=1 ; i<=7 ;i++ )
    {
           for( int j=0 ; j<=6 ; j++ )
       {
                if( j==col )
                {
          for( int n=1 ; n<= 2*i-1 ; n++)
              System.out.print( "*" );                    
          System.out.println( );
          break;
                }
        else                    
            System.out.print( " " );
        }
            
    col--;

    }

}