import java.util.Scanner;/** * A class that creates a magic square given the side length. */public class MagicSquare{ public static void main(String[] args) // calls for args in the prompt. { Scanner in = new Scanner(System.in); System.out.print("How large do you want this square to be? (n x n): "); int n = in.nextInt(); if (n % 2 == 0) throw new RuntimeException("n must be odd"); // throw error message int[][] magic = new int[n][n]; int row = n-1; int col = n/2; magic[row][col] = 1; // place 1 in the middle of the bottom column for (int i = 2; i <= n*n; i++) { if (magic[(row + 1) % n][(col + 1) % n] == 0) { row = (row + 1) % n; col = (col + 1) % n; } else { row = (row - 1 + n) % n; // don't change col } magic[row][col] = i; } // print results for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (magic[i][j] < 10) System.out.print(" "); // spaces for alignment if (magic[i][j] < 100) System.out.print(" "); System.out.print(magic[i][j] + " "); } System.out.println(); } }}