+ 2
how to make a star pattern in java
4 Answers
+ 1
use for loop
+ 1
*
***
*****
*******
*****
***
*
we could use recursive function to atchieve that.
try the following program in your PC
ex:
public class Program
{
public static void drawDiamond(int levels){
drawDiamond(levels,1,false);
}
private static void drawDiamond(int levels,int it,boolean reverse){
if(it==0) return;
if(it>levels){
reverse =true;
it=it-2;
}
if(it<levels){
System.out.print(String.format("%"+(levels-it)+"s",""));
}
int stars=it*2-1;
for(int i=0;i<stars;i++){
System.out.print("*");
}
System.out.println();
if(reverse){
drawDiamond(levels,--it,true);
}else{
drawDiamond(levels,++it,false);
}
}
public static void main(String[] args) {
drawDiamond(4);
}
}
- 1
can you give details on what star pattern
- 1
a dimond shape