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

用java语言编写一个打印出钻石形状的图形的程序
求解:用java语言编写一个打印出钻石形状的图形的程序,要求使用嵌套
图形如下:
                                *
                              ***
                            *****
                          *******
                            *****
                              ***
                                *


------解决方案--------------------
public class Print
{
public static void main(String[] args)
{
for(int i=0;i <4;i++)
{
for(int j=0;j <7;j++)
{
if(j> =3-i&&j <=3+i)
{
System.out.print( "* ");
}

else
{
System.out.print( " ");
}

}
System.out.println( " ");
}


for(int i=2;i> =0;i--)
{
for(int j=0;j <7;j++)
{
if(j> =3-i&&j <=3+i)
{
System.out.print( "* ");
}

else
{
System.out.print( " ");
}

}
System.out.println( " ");
}
}
}
------解决方案--------------------
class test{
public static void main(String[] args){
int k=0;
for (int i=0;i <7;i++)
{
for(int j=0;j <7;j++)
{
if(i <4)
{
if ((j> =3-i)&&(j <=i+3))
System.out.print( "* ");
else
System.out.print( " ");
}
else
{
k=7-i;
if ((j> =4-k)&&(j <=k+2))
System.out.print( "* ");
else
System.out.print( " ");
}
}
System.out.println( " ");
}
}
}
------解决方案--------------------
看了大家都写2个循环的
我不写循环 写了个函数递归调用的,并可以从命令行输入最大行的参数,测试通过
我想LZ要的是我这种吧

class TestStar{
static int count=1;
static int i=1;
public static void output(int n){
if(count <=2*i-1){
System.out.print( "* ");
count++;
output(n);
}else if(i <n){
System.out.println( " ");
count=1;
i++;
output(n);
}else if(i> 0){
System.out.println( " ");
count=1;
i--;
n--;
output(n);
}
}

public static void main(String[] args){
try{
int n=Integer.parseInt(args[0]);
TestStar.output(n);
}catch(Exception e){
System.out.println( "请输入参数! ");
}
}
}
------解决方案--------------------
楼上的,楼主的图形应该是一个菱形,因为CSDN不支持缩进才显示成了三角形。我写的: