Magic Squares A n x n matrix that is filled with numbers 1, 2, 3….n2. The sum of the…
- User
Magic Squares
A n x n matrix that is filled with numbers 1, 2, 3….n2. The sum of the elements in each row, column and the two diagonals is the same value. Write a program that reads in n2 values from the keyboard and tests to see if they form a magic square when arranged in a square matrix. You need to test for three conditions: Did the user enter n2 numbers for some n? Do each of the numbers 1, 2, 3….n2 occur exactly once in the user input? When put into a square, are the sums of the rows, columns and diagonals equal to each other?
IF the size of the input is a square, then test whether all numbers between 1 and n2 are present. Then compute the row, column and diagonal sums. Implement a class Square with methods public void add(int i) public boolean isMagic()
- Assistant
- User
ran into some errors with this code: import java.util.Scanner; public class Tester { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); List inputList = new ArrayList<>(); System.out.println("Enter the numbers: "); while (scanner.hasNextInt()) { inputList.add(scanner.nextInt()); } scanner.close();
codeint inputSize = inputList.size(); int n = (int) Math.sqrt(inputSize); if (n * n != inputSize) { System.out.println("Not a valid square input"); return; } Square square = new Square(n); for (int num : inputList) { square.add(num); } if (square.isMagic()) { System.out.println("It's a magic square."); } else { System.out.println("It's not a magic square."); } }}
- Assistant
- User
11 18 25 2 9 10 12 19 21 3 4 6 13 20 22 23 5 7 14 16 17 24 1 8 15 this is my magic square how would input it
- Assistant
- User
if i type in the all the numbers the code doesn't stop asking for numbers
- Assistant
- User
when i type in type all the number and press enter the code keeps taking in numbers
- Assistant