*
print this pattern using only 3 loops, i used this method plz anyone can optimize this code.
public class Test {
static int z =0;
public void paten(int l)
{
for(int b=l; b<z ; b++)
{
System.out.print(" ");
}
for(int c=1; c<=(l*2)-1; c++)
{
System.out.print("*");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = n/2 +1;
z=k;
int o=0;
Test t = new Test();
for(int i = n ; i>=1; i--){
try{Thread.sleep(100);}
catch(Exception e)
{
}
if(k>1)
{
t.paten(k);
System.out.println("");
k--;
}
else if(k==1)
{
t.paten(k);
System.out.println("");
o=k+1;
k--;
}
else{
t.paten(o);
System.out.println("");
o++;
}
}
}
}
print this pattern using only 3 loops, i used this method plz anyone can optimize this code. or tells me the best optimal solution of this using three loop and can reduce the number of lines of code.
Aucun commentaire:
Enregistrer un commentaire