Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package jepeffer_Magic.magic;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- public class jepeffer_Magic {
- public static void main(String[] args) throws Exception {
- Scanner get = new Scanner(System.in);
- System.out.println("Enter the file name");
- String inFile;
- inFile = get.next();
- int magic = 0;
- File file = new File(inFile);
- Scanner in = new Scanner(file);
- int column = in.nextInt();
- int mn[][] = new int[column][column];
- for (int i = 0; i < column; i++) {
- for (int y = 0; y < column; y++) {
- mn[i][y] = in.nextInt();
- magic = mn[i][y] + magic;
- }
- }
- magic = magic / column;
- display(mn, magic);
- System.out.println();
- rowCheck(mn, magic);
- colCheck(mn, magic);
- diagCheck(mn, magic);
- boolean unique = uniqueCheck(mn);
- if ( unique = false) {
- System.out.println("false");
- }
- }
- public static boolean uniqueCheck(int[][] x) throws Exception {
- int counter = 0;
- for (int row = 0; row < x.length; row++) {
- for (int col = 0; col < x.length; col++) {
- int num = x[row][col];
- for (int otherCol = col + 1; otherCol < x.length; otherCol++) {
- if (num == x[row][otherCol]) {
- return false;
- }
- }
- }
- }
- return true;
- }
- private static void diagCheck(int x[][], int magic) throws Exception {
- System.out.println();
- System.out.println();
- int diag1 = 0;
- int diag2 = 0;
- int n = x.length;
- for (int i = 0; i < x.length; i++) {
- for (int j = 0; j < x.length; j++) {
- if (j == i) {
- diag1 += x[i][j];
- }
- if (diag1 == magic) {
- System.out.println("DIAG 1: GOOD");
- }
- }
- }
- // for (int i = x.length -1; i >= 0 ; i--) {
- // for (int j = x.length - 1; j >= 0; j--) {
- // for (int i = 0; i < x.length; i++) {
- // for (int j = 0; j < x.length; j++) {
- n = x.length - 1;
- for (int j = 0; j < x.length; j++) {
- if (n >= 0) {
- diag2 += x[j][n];
- // System.out.print(" " + x[j][n]);
- // System.out.print(" " + diag2);
- n--;
- }
- if (diag2 == magic) {
- System.out.println("DIAG 2: GOOD");
- }
- }
- }
- private static void colCheck(int[][] x, int magic) throws Exception {
- System.out.println();
- int tracker = 0;
- int counter = 0;
- for (int c = 0; c < x.length; c++) {
- System.out.println();
- for (int r = 0; r < x.length; r++) {
- counter = x[r][c] + counter;
- }
- if (magic == counter) {
- tracker++;
- System.out.printf("COL %d IS: GOOD", tracker);
- counter = 0;
- } else {
- tracker++;
- System.out.printf("COL %d IS BAD: (%d)", tracker, counter);
- counter = 0;
- }
- }
- }
- private static void rowCheck(int[][] x, int magic) throws Exception {
- int tracker = 0;
- int column = 0;
- int counter = 0;
- int rows = x.length;
- int columns = x.length;
- for (int i = 0; i < rows; i++) {
- System.out.println();
- for (int y = 0; y < columns; y++) {
- tracker = x[i][y] + tracker;
- // System.out.print(tracker);
- }
- if (tracker == magic) {
- counter++;
- System.out.printf("ROW %d IS: GOOD ", counter);
- } else {
- System.out.printf("ROW %d IS BAD: (%d)", counter, tracker);
- counter++;
- }
- tracker = 0;
- }
- }
- public static void display(int x[][], int y) throws Exception {
- int column = x.length;
- int counter = 0;
- int d = 0;
- System.out.printf("Checking a %d x %d Magic Square!%n", column, column);
- System.out.printf("The magic number is %d%n", y);
- for (int f = 0; f < x.length; f++) {
- if (d == 0) {
- System.out.print(" ");
- System.out.printf("[%2d] ", d + 1);
- d = 1;
- } else {
- System.out.printf("[%2d] ", d + 1);
- d++;
- }
- }
- int rows = x.length;
- int columns = x.length;
- for (int i = 0; i < rows; i++) {
- System.out.println();
- for (int j = 0; j < columns; j++) {
- if (counter == 0) {
- System.out.printf("[%2d]", i + 1);
- counter = 1;
- }
- System.out.printf("%4d ", x[i][j]);
- }
- counter = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement