Advertisement
Skootiebae

Untitled

Nov 22nd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. //Original lab 3 text
  2. import java.util.Scanner;
  3. public class Main {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  
  8. int magicSquare[][] = new int[4][4];
  9. int i, j, x, y;
  10. int total;
  11. int getNum;
  12. boolean valid = true;
  13.  
  14. Scanner obj = new Scanner(System.in);
  15. //User Enters values into array, second set of nested loops
  16. // checks if the value is in the required range and not duplicating
  17.  
  18. for (i = 0; i < 4; i++) {
  19. for (j = 0; j < 4; j++) {
  20. while (valid) {
  21. System.out.println("Please fill in element: " + i + " " + j);
  22. getNum = obj.nextInt();
  23. for (x = 0; x < 4; x++) {
  24. for (y = 0; y < 4; y++) {
  25. if (magicSquare[x][y] = getNum;valid = false){
  26. if (valid) (magicSquare[i][j] = getNum )
  27.  
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34.  
  35. /*
  36. for ( i = 0; i < 4; i++) {
  37. for ( j = 0; j < 4; j++) {
  38. while (true){
  39. System.out.println("Please fill in element: " + i + " " + j);
  40. magicSquare[i][j] = obj.nextInt();
  41. for (x=0; x<4; x++){
  42. for(y=0;y<4;y++){
  43. if magicSquare[x][y] = magicSquare,
  44. checkNum = magicSquare[i][j];
  45. if (checkNum < 1 || checkNum > 16)
  46. if magicSquare[x][y] = checkNum, valid = false
  47. if(valid) magicSquare[i][j]=checkNum
  48.  
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55.  
  56.  
  57. this was just another Idea i was messing around with, I'm confused with the logic, haven't been able to compile :(
  58. */
  59.  
  60.  
  61.  
  62.  
  63. //Magic Square must = 34 in all directions
  64. //Math check
  65.  
  66. // Find and check the sum for each row.
  67.  
  68. for (i = 0; i < 4; i++) {
  69.  
  70. total = 0;
  71. for (j = 0; j < 4; j++)
  72. total += magicSquare[i][j];
  73.  
  74. if (total != 34)
  75. System.out.println("Rows are not magical!");
  76.  
  77. }
  78. // Find and check the sum for each column.
  79.  
  80. for (j = 0; j < 4; j++) {
  81. total = 0;
  82. for (i = 0; i < 4; i++)
  83. total += magicSquare[i][j];
  84.  
  85.  
  86.  
  87. }
  88.  
  89. // Check diagonals
  90. if (magicSquare[0][0] + magicSquare[1][1] + magicSquare[2][2] + magicSquare[3][3] != 34)
  91. System.out.println("Diagonal Down is not magical!");
  92. if (magicSquare[3][0] + magicSquare[2][1] + magicSquare[1][2] + magicSquare[0][3] != 34)
  93. System.out.println("Diagonal up is not magical!");
  94.  
  95.  
  96. //Print out 2D array 'magicalSquare'
  97. for (i = 0; i < 4; i++) {
  98. for (j = 0; j < 4; j++) {
  99. System.out.print(magicSquare[i][j] + "\t");
  100. }
  101. System.out.println();
  102. }
  103.  
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement