Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

magicsquare

807603Mar 17 2007 — edited Jan 27 2008
i have to create a program to draw a magic square using a 2 dimensional array

This is my class so far it should only create part of a magic square

//MagicSquare.Java
//A MagicSquare class for producing a magic square
//This class produces a 2 dimensional array of size by n and sets all values to 0
//It is a (n*n) magic sqaure where n is an odd integer

public class MagicSquare {
public static void main (String[] args){

System.out.println("This is a magic square application");

int n = 3;

//I will set n to 3. It is also an odd interger as required

int [][]magsqr = new int[n][n];

//I have created an array
//I have used two set of square brackets so the array is two dimensional as required

int x = 1;
int y =((n+1)/2)-1;

magsqr [x][y] = 1;

//I am now carrying out the formula as stated in the question

for (int i=2; i <= (n*n);i++){

if(magsqr[((x-1)+n)%n]][((y-1)+n)%n]){

x = ((x-1))%n;
y = ((y-1))%n;

}
else {

x = x+1;
}
}
}
}

i have the following error

C:\Documents and Settings\jon c\My Documents\java 124\MagicSquare.java:29: ')' expected
if(magsqr[((x-1)+n)%n]][((y-1)+n)%n]){
^
C:\Documents and Settings\jon c\My Documents\java 124\MagicSquare.java:39: illegal start of expression
}
^
2 errors

Tool completed with exit code 1

Can anyone help me with this. Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 24 2008
Added on Mar 17 2007
5 comments
209 views