Kamis, 20 November 2008

Source code Java Diamond pattern

Berikut ini adalah source code untuk membuat diamond pattern (pola gambar diamond).


public class DiamondPattern
{ private int number = 0;

public DiamondPattern(int number)
{
this.number = number;
}

static void test()
{
DiamondPattern t = new DiamondPattern(9);
t.printDiamond();
}
public static void main(String[] args)
{
test();
}
public void printDiamond()
{int k =1;
int m = 0;
//segitiga atas
for(int i=1;i<=number/2+1;i++)
{
for(int j=number;j>m;j--)
{
System.out.print(" ");
}
for(int z=1;z<=k;z++)
{
System.out.print("*");
System.out.print(" ");
}
System.out.println(" ");
k=k+2;
m=m+2;
}

m=2;
k=7;
for(int i=1;i<=number/2;i++)
{
for(int j=0;j<=m;j++)
{
// System.out.print(j);
System.out.print(" ");
}
for(int z=1;z<=k;z++)
{
System.out.print("*");
System.out.print(" ");
}
System.out.println(" ");
m=m+2;
k=k-2;
}
}


}

Tidak ada komentar: