如何在Java程序中用*输出一个菱形的问题!
1.这样的如下图:
*
* * *
* * * * *
* * *
*
2.这样的如下图:
* * * * *
* * * *
* * *
* *
*
谢谢了..!
------解决方案-------------------- 1.这样的如下图:
*
* * *
* * * * *
* * *
*
public void printAngle(int n) //n=3
{
for(int i=1;i <n+1;i++)
{
for(int j=i;j <n+1;j++)
System.out.print( " ");
for(int k=1;k <i+1;k++)
System.out.println( "* ");//System.out.print(k);
for(int m=i-1;m> 0;m--)
System.out.println( "* ");//System.out.print(m);
System.out.println();
}
for(int i=n-1;i> 0;i--)
{
for(int m=1;m <n-i+2;m++)
System.out.print( " ");
for(int k=1;k <i+1;k++)
System.out.println( "* ");//System.out.print(k);
for(int j=i-1;j> 0;j--)
System.out.println( "* ");//System.out.print(j);
System.out.println();
}
}
2.这样的如下图:
* * * * *
* * * *
* * *
* *
*
public void printRecAngel(int n) //n=5
{
for(int i=1;i <n+1;i++)
{
for(int j=1;j <i;j++)
System.out.print( " ");
for(int j=i;j <n+1;j++)
System.out.print( "* ");//System.out.print(j);
System.out.println();
}
}
------解决方案--------------------class Print{
public static void main(String arg[]){
printRecAngel();
printRecAngel();
}
public void printRecAngel()
{
System.out.println( " * " );
System.out.println( " * * * ");
System.out.println( " * * * * * ");
System.out.println( " * * * ");
System.out.println( " * ");
}
public void printRecAngel(){
System.out.println( "* * * * * ");
System.out.println( " * * * * ");
System.out.println( " * * * ");
System.out.println( " * * ");
System.out.println( " * ");
}
}
------解决方案--------------------1.这样的如下图:
*
* * *
* * * * *
* * *
*
public class A
{
public static void main(String[] args)
{
for(int i=1;i <=3;i++)
{
for(int j=1;j <=6-2*i;j++)
System.out.print( " ");
for(int j=1;j <=2*i-1;j++)
System.out.print( "* ");
System.out.println(); //打印上三角
}
for(int i=1;i <=2;i++)
{
for(int j=1;j <=2*i;j++)
System.out.print( " ");
for(int j=1;j <=5-2*i;j++)
System.out.print( "* ");
System.out.println();