In: Computer Science
I have an 8 by 8 array that has values ranging from 1-64. I am wanting to display the array evenly but due to some digits being single and others double, it displays lopsided. What would be the best way to get everything in the array to display in a nice even square?
CODE IN JAVA:
Square.java file:
public class Square {
public static int digitCount(int n) {
int count = 0 ;
while(n!=0) {
n = n/10 ;
count += 1
;
}
return count ;
}
public static void main(String[] args) {
// TODO Auto-generated method
stub
int arr[][] = new int[8][8];
int count = 1 ;
for(int i=0;i<8;i++) {
for(int
j=0;j<8;j++) {
arr[i][j] = count ;
count +=1 ;
}
}
for(int i=0;i<8;i++) {
for(int
j=0;j<8;j++) {
System.out.print(" "+arr[i][j]+" ");
if(digitCount(arr[i][j])==1)
System.out.print(" ");
}
System.out.println("");
}
}
}
OUTPUT: