Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Magic Square

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Magic Square

    need help in an assignment
    here is what i got so far! after this im having a hard time figuring it out!

    here is my code:

    public class Start {

    public static void main(String [] args){
    Scanner sc = new Scanner(System.in);
    int num = 1;
    int input;
    System.out.println("Enter number:");
    input = sc.nextInt();
    int limit = input;
    int a = input/2;
    int b = 0;
    int [][] arr = new int[input][input];
    do{
    for( num = 1; num<=limit; num++){
    if(arr[b][a]==0){
    arr[b][a] = num;
    if(b == 0){
    b = input;
    }
    if(a == input -1){
    a=-1;

    }
    a = a+1;
    b = b-1;

    }

    else{
    b=b+2;
    a = a-1;
    }
    if(b == input){
    b = 0;
    }
    if(b > input){
    b =1;
    }
    if(a == -1){
    a = input-1;
    }
    arr[b][a] = num;

    }
    num++;
    }while(num <= limit);

    for(int x =0;x<input;x++){
    for(int y = 0;y<input;y++){
    System.out.print(arr[x][y]);

    }
    System.out.println(" ");
    }

    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Magic Square

    Welcome to the forum! Please read this topic to learn how to post code correctly along with other useful info for newcomers.

    Here's some general guidance that may or may not be included in the link I've given you: Don't just post broken or useless code and expect us to figure out what it's supposed to do and then fix it for you. Tell us what's wrong with it, post any errors you're getting that you can't fix yourself, describe the code's current behavior and how that needs to change, ask specific questions.

    Read the link, fix the above post, and follow the other suggestions I've given you.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Magic Square

    Also posted at: need help for magic square
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 10
    Last Post: April 21st, 2013, 09:28 AM
  2. JAVA MAGIC SQUARE
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 28th, 2012, 12:42 AM
  3. Type magic
    By meathead in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 19th, 2011, 12:12 PM
  4. Magic Square Checker
    By Hypnos in forum Object Oriented Programming
    Replies: 1
    Last Post: April 3rd, 2011, 06:37 PM
  5. Magic square class (modular math problem)
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 30th, 2010, 10:56 AM