-2

I'm creating a java project called magicsquare and I've searched online on how to do it. Now, I'm trying to understand how the 2nd loop works, I know that it prints and align the magic square, but I don't know the details. I already know the first one. I would really appreciate if someone explains to me the 2nd loop. Thanks!

import java.util.*;
public class Magicsquare {

        public static void main(String[] args) { 
            Scanner input = new Scanner(System.in);

     try{
            int N;
            System.out.print("Enter a number to create a Magic Square: ");
            N=input.nextInt();

            if (N % 2 == 0){
                System.out.print("N must be an Odd number!");
            }

            else{

            int[][] magic = new int[N][N];
            int row = N-1;
            int col = N/2;
            magic[row][col] = 1;

            for (int i = 2; i <= N*N; i++) {
                if (magic[(row + 1) % N][(col + 1) % N] == 0) {
                    row = (row + 1) % N;
                    col = (col + 1) % N;
                }
                else {
                    row = (row - 1 + N) % N;

                }
                magic[row][col] = i;
            }


            for (int c = 0; c < N; c++) {
                 for (int r = 0; r < N; r++) {
                    if (magic[r][c] < 10)  System.out.print(" ");  // for alignment
                    if (magic[r][c] < 100) System.out.print(" ");  // for alignment
                    System.out.print(magic[r][c] + " ");
                }
                System.out.println();
          } 
       }main (null);
    }catch (Exception e){
        System.out.print("Invalid Input!");
    }
  }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Carsh CaNe
  • 39
  • 7
  • Where did you find this code? – Hovercraft Full Of Eels Sep 17 '15 at 21:08
  • I found it in http://introcs.cs.princeton.edu/java/14array/MagicSquare.java.html but i slightly tweaked it .. – Carsh CaNe Sep 17 '15 at 21:10
  • I've edited it to show the full code I've created. – Carsh CaNe Sep 17 '15 at 21:12
  • What *specifically* confuses you about the nested for loops? the printing of spaces? Myself I'd use `System.out.printf(...)` to simplify things, but this would work too. – Hovercraft Full Of Eels Sep 17 '15 at 21:13
  • are you able to explain line by line on the 2nd loop? I would really appreciate it. – Carsh CaNe Sep 17 '15 at 21:16
  • Best for you to work this out on paper. I assume that you already know the difference between print and println, but in case you don't, print prints the String, and println prints the line followed by a new-line (carriage return if this were a typewriter). Printing `" "` prints spaces. I am confident that if you walk through this code on paper, you will figure this out. Voting to close this question, because you're really much better off working this out, and you have the ability to do it too. – Hovercraft Full Of Eels Sep 17 '15 at 21:17
  • 3
    It would be a better exercise for you to improve your understanding by yourself: try running the code with each line commented out in turn, see how its behaviour changes. – Andy Turner Sep 17 '15 at 21:17

1 Answers1

0

Well, first the obvious. The part about < 10 and < 100: if a number is between 0 and 9, it's only going to print out one digit. If it's between 10 and 99, it's going to print out two. And if it's between 100 and 999, it'll print out using three digits. (It seems as if this code is written to assume it will only encounter numbers between 0 and 999. Generally speaking, it's best to ensure that somehow rather than just hope.)

So, with the if statements and their extra spaces, a "5" will print out as " 5" (note the two leading spaces for a total of three characters). 25 will print out as " 25" (again, three characters) and 125 as "125" (three digits again). Since all of the numbers print out using three characters, everything will line up neatly in columns.

What confuses me is that you're iterating over c first, then r. This seems to say that you're printing out the first column on a single row on the screen, then the second column as a second row, and the third column as a third row. I.e. the whole thing has been rotated on a diagonal. But maybe that's just a naming issue.

Erick G. Hagstrom
  • 4,873
  • 1
  • 24
  • 38
  • @CarshCaNe: Please be careful that you don't cheat yourself by not trying to solve this yourself first. – Hovercraft Full Of Eels Sep 17 '15 at 21:31
  • @Hovercraft Full of Eels - I've been figuring this out by myself for hours already but the lack of training hinders me of fully understand this code. I just started learning java for 2 months now minus the working time that I have since I'm not a professional programmer, I work at a company that is far out from Java programming. I'm trying to learn java programming because it interests me. I'm sorry if I'm not as genius as you. – Carsh CaNe Sep 17 '15 at 21:49