开发者

Magic Square Java program

//Kevin Clement //Week3A Magic Squares

Hey all, doing an introductory assignment to 2dimensional arrays. Below is the code I have done which is pretty much done.

My problem I get is I'm not entirely sure how to print out the array, as well as getting everything to run right with a test method. I get an error out of bounds at the line msq[order][order] = 1;

I apologize if my formatting of question is wrong, still not used to this site. Any help would be great. Thanks!

import java.util.*;

class Magic
{
  private int order;
  int msq[开发者_高级运维 ][ ];

  public Magic(int size)
  {
     if(size < 1 || size % 2 == 0)
     {
        System.out.print("Order out of range");
        order = 3;
     }
     msq = new int[order][order];
     Build();
     Display();
   }

  public void Build()
   {
     int row = 0;
     int col =0;
    msq[order][order] = 1;

     for(int k = 1; k <= order * order; k++)
     {
        msq[row][col] = k;

        if(row == 0 && col == order -1)
           row++;
        else if(row == 0)
        {
           row = order - 1;
           col++;
        }
        else if(msq[row - 1][col + 1] != 0)
           row++;
        else if(msq[row -1][col + 1] == 0)
        {
           row--;
           col++;
        }
        if(col == order - 1)
        {
           col = 0;
           row--;
        }

     }
  }
  public void Display()
  {

     for(int i = 0; i < order; i++)
     {
        for(int k = 0; k < order; k++)
        {
           System.out.println(msq[i][k] + " ");
        }
     }


  }
}


I get an error out of bounds at the line msq[order][order] = 1;

msq = new int[order][order];
// ..
msq[order][order] = 1;

If array size is n, then you need to access the elements from 0 to n-1. There is no nth index. In your case there is no order, order index. It is only from 0 to order-1 and is the reason for array index out of bounds exception.


What is the reason for this condition in the constructor?:

if(size < 1 || size % 2 == 0)
 {
    System.out.print("Order out of range");
    order = 3;
 }

Note that whenever you use a size input that doesn't satisfy the if clause, the variable order is not initialized and defaults to 0. As a result the 2d array has size zero and throws the out of bounds error. If you are trying to use 3 as a default value, then u want to move the line:

order = 3;

before the if block.

Other things to consider: 1. make the order variable final since u don't plan on changing it. Eclipse IDE would warn you about the situation described above if you do so.

or 2. If you are going to default to 3 for the value of order initialize it as such.

private int order = 3

Also you might consider printing a message saying order defaults to three when the condition is not satisfied.


To print a matrix of integers in Java

for (int i = 0; i < order; i++) {
    for (int k = 0; k < order; k++) {
       System.out.printf("%6d", msq[i][k]);
    }
    System.out.println();
 }

The second part of your question is answered by Mahesh.


msq[order][(order] = 1;

--> here is a syntax error. You have an '('. You get end of bound error because array start from 0 not 1 (which is a mistake every beginner makes) therefore change it to msq[order-1][order-1] = 1;

The answer above is the correct way to print out the array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜