Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. package jepeffer_Magic.magic;
  2.  
  3. import java.util.Scanner;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6.  
  7. public class jepeffer_Magic {
  8.  
  9. public static void main(String[] args) throws Exception {
  10.  
  11. Scanner get = new Scanner(System.in);
  12. System.out.println("Enter the file name");
  13. String inFile;
  14. inFile = get.next();
  15. int magic = 0;
  16. File file = new File(inFile);
  17. Scanner in = new Scanner(file);
  18.  
  19. int column = in.nextInt();
  20. int mn[][] = new int[column][column];
  21. for (int i = 0; i < column; i++) {
  22. for (int y = 0; y < column; y++) {
  23.  
  24. mn[i][y] = in.nextInt();
  25. magic = mn[i][y] + magic;
  26. }
  27.  
  28. }
  29. magic = magic / column;
  30. display(mn, magic);
  31. System.out.println();
  32. rowCheck(mn, magic);
  33. colCheck(mn, magic);
  34. diagCheck(mn, magic);
  35. boolean unique = uniqueCheck(mn);
  36.  
  37. if ( unique = false) {
  38. System.out.println("false");
  39.  
  40. }
  41.  
  42. }
  43.  
  44. public static boolean uniqueCheck(int[][] x) throws Exception {
  45. int counter = 0;
  46. for (int row = 0; row < x.length; row++) {
  47. for (int col = 0; col < x.length; col++) {
  48. int num = x[row][col];
  49. for (int otherCol = col + 1; otherCol < x.length; otherCol++) {
  50. if (num == x[row][otherCol]) {
  51. return false;
  52.  
  53. }
  54.  
  55. }
  56. }
  57. }
  58. return true;
  59.  
  60. }
  61.  
  62.  
  63. private static void diagCheck(int x[][], int magic) throws Exception {
  64. System.out.println();
  65. System.out.println();
  66. int diag1 = 0;
  67. int diag2 = 0;
  68. int n = x.length;
  69. for (int i = 0; i < x.length; i++) {
  70. for (int j = 0; j < x.length; j++) {
  71. if (j == i) {
  72. diag1 += x[i][j];
  73. }
  74. if (diag1 == magic) {
  75. System.out.println("DIAG 1: GOOD");
  76. }
  77. }
  78. }
  79.  
  80. // for (int i = x.length -1; i >= 0 ; i--) {
  81. // for (int j = x.length - 1; j >= 0; j--) {
  82. // for (int i = 0; i < x.length; i++) {
  83. // for (int j = 0; j < x.length; j++) {
  84. n = x.length - 1;
  85. for (int j = 0; j < x.length; j++) {
  86. if (n >= 0) {
  87. diag2 += x[j][n];
  88. // System.out.print(" " + x[j][n]);
  89. // System.out.print(" " + diag2);
  90. n--;
  91. }
  92. if (diag2 == magic) {
  93.  
  94. System.out.println("DIAG 2: GOOD");
  95. }
  96. }
  97.  
  98. }
  99.  
  100. private static void colCheck(int[][] x, int magic) throws Exception {
  101. System.out.println();
  102. int tracker = 0;
  103. int counter = 0;
  104. for (int c = 0; c < x.length; c++) {
  105. System.out.println();
  106. for (int r = 0; r < x.length; r++) {
  107. counter = x[r][c] + counter;
  108. }
  109. if (magic == counter) {
  110. tracker++;
  111. System.out.printf("COL %d IS: GOOD", tracker);
  112.  
  113. counter = 0;
  114. } else {
  115. tracker++;
  116. System.out.printf("COL %d IS BAD: (%d)", tracker, counter);
  117. counter = 0;
  118.  
  119. }
  120. }
  121.  
  122. }
  123.  
  124. private static void rowCheck(int[][] x, int magic) throws Exception {
  125. int tracker = 0;
  126. int column = 0;
  127. int counter = 0;
  128.  
  129. int rows = x.length;
  130. int columns = x.length;
  131. for (int i = 0; i < rows; i++) {
  132. System.out.println();
  133. for (int y = 0; y < columns; y++) {
  134. tracker = x[i][y] + tracker;
  135. // System.out.print(tracker);
  136.  
  137. }
  138. if (tracker == magic) {
  139. counter++;
  140. System.out.printf("ROW %d IS: GOOD ", counter);
  141. } else {
  142. System.out.printf("ROW %d IS BAD: (%d)", counter, tracker);
  143. counter++;
  144.  
  145. }
  146. tracker = 0;
  147. }
  148.  
  149. }
  150.  
  151. public static void display(int x[][], int y) throws Exception {
  152.  
  153. int column = x.length;
  154. int counter = 0;
  155. int d = 0;
  156. System.out.printf("Checking a %d x %d Magic Square!%n", column, column);
  157. System.out.printf("The magic number is %d%n", y);
  158. for (int f = 0; f < x.length; f++) {
  159. if (d == 0) {
  160. System.out.print(" ");
  161. System.out.printf("[%2d] ", d + 1);
  162. d = 1;
  163.  
  164. } else {
  165. System.out.printf("[%2d] ", d + 1);
  166. d++;
  167. }
  168. }
  169.  
  170. int rows = x.length;
  171. int columns = x.length;
  172.  
  173. for (int i = 0; i < rows; i++) {
  174.  
  175. System.out.println();
  176. for (int j = 0; j < columns; j++) {
  177. if (counter == 0) {
  178. System.out.printf("[%2d]", i + 1);
  179. counter = 1;
  180. }
  181. System.out.printf("%4d ", x[i][j]);
  182. }
  183. counter = 0;
  184.  
  185. }
  186.  
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement