/ Published in: Java
Expand |
Embed | Plain Text
import java.util.Scanner; public class MagicSquares { public static int userInput; private static int g,c; public int[][] matrix; private int ROW = 3; private int COLUMNS = 3; private int x,z,m,a,b,d; private int sumHolizontal; private int sumVertical; private int sumCross; public static boolean booleanVar; public MagicSquares() { matrix = new int [ROW][COLUMNS]; userInput = 0; m=1; a=0; b=0; c=0; d=1; } public static int getUserInput() { return sc.nextInt(); } public void add(int i) { matrix[z][x] = i; ++x; if(x == 3) { z++; x=0; } m++; } public void printData() { if(d==3) else ++b; if(b == 3) { a++; b=0; d=0; } d++; } public boolean isMagic() { sumHolizontal = matrix[0][0] + matrix[0][1] + matrix[0][2]; sumVertical = matrix[0][0] + matrix[1][0] + matrix[2][0]; sumCross = matrix[0][0] + matrix[1][1] + matrix[2][2]; return sumHolizontal == sumVertical; } { MagicSquares ms = new MagicSquares(); for(g = 0; g< 9; g++) { userInput = getUserInput(); ms.add(userInput); } /** using a for loop print number */ for(c = 0; c<9;c++) { ms.printData(); } /** check bool */ booleanVar = ms.isMagic(); } }
You need to login to post a comment.
