
import java.util.*;
public class MagicSquare
{
public static void main(String[]args)
{
String magicSquare = "";
int size;
int[] arrayNumbers = {2,3,4,5,6,7,8,9};
Random randomNumber = new Random();
Scanner input = new Scanner(System.in);
do
{
System.out.printf("Enter an odd number: ");
size = input.nextInt();
}while(size < 0 && (size%2)==0);
System.out.printf("Creating a magic square with the size of %d\n", size);
//create first line before the 1
for(int i = 0;i<size/2;i++)
{
magicSquare += arrayNumbers[randomNumber.nextInt(arrayNumbers.length)];
}
magicSquare += "1";
for(int i = 0;i<size/2; i++)
{
magicSquare += arrayNumbers[randomNumber.nextInt(arrayNumbers.length)];
}
magicSquare += "\n";
//first line created
//create the rest of the lines
for(int i = 0; i < size-1;i++)
{
for(int j = 0;j<size;j++)
{
magicSquare += arrayNumbers[randomNumber.nextInt(arrayNumbers.length)];
}
magicSquare += "\n";
}
System.out.printf("The magic square has been completed:\n%s",magicSquare);
}
}