本人是新学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用得太多了,有其他办法完成没有,谢谢各位。
我自己写了一个有控制面板画出三角形,
代码如下:
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用得太多了,有其他办法完成没有,谢谢各位。
{
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<(2*a + 1) ;c++ )
{
System.out.print("*");
}
// for (int d=1;d<=a ;d++ ) //多余代码
// {
// System.out.print("*");
// }
System.out.println();
}
}
}这位大神啊,像这种题目跳过去就可以了,多学学运用,画画三角形貌似没什么用,你上面多用了很多代码,其实我并没有优化什么,只是代码简洁了一些,你觉得呢?
{
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--; }}
class day03 {
public static void main(String[] args) {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 6 - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i * 2 + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}