I'm printing a diamond shape in Java. It's working fine, but is there a way to re-write this code to make it faster?
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
String ns = String.valueOf(n);
int z = n/2;
int k = n%2;
for (int j = 0 ; j<n;j++) {
for (int i = 0; i < z; i++)
System.out.print(" ");
for (int i = 0; i < k ; i++){
System.out.print("*");
if (i==k-1)
System.out.print("\n");
}
if ((k-n)==0)
break;
z--;
k=k+2;
}
z = (n)/2;
k = (n)%2;
int w = k;
int y = n-2;
for (int j = 0 ; j<z ;j++) {
for (int i = 0 ; i<w;i++)
System.out.print(" ");
for (int i = 0 ; i< y ;i++)
System.out.print("*");
w=w+1;
y=y-2;
System.out.print("\n");
}