0

これは、2D 配列を扱う AP コンピューター サイエンス クラスの宿題です。基本的に、プログラムは魔方陣に値を入力し (すべての行、列、および対角線の合計が同じ数になります)、それが魔方陣であるかどうかを判断します。私のアルゴリズムは、Magic かどうかをチェックするときに、(isMagic() メソッドに従って) false を出し続けます。row() および column() メソッドに問題があると思います。誰かが私に解決策を提供してくれれば、それは素晴らしいことです。

public class Square
{
  private int [][] sq;
  private int position = 0;

  public Square(int size)
  {
    sq = new int[size][size];
  }

  /**
   * adds value to the matrix at the given position, row,col
   */

  public void add(int value, int row, int col)
  {
    sq[row][col] = value;
    // fill in code here.

  }

  public boolean fullSquare()
  {
    int numcheck = 1;
    boolean found = false;
    while (numcheck < sq.length*sq.length)
    {
      for(int i = 0; i < sq.length; i++)
        for(int j = 0; j < sq.length; j++)
      {
        if(sq[i][j] == numcheck)
        {
          found = true;
        }
      }
      numcheck++;

      //use nested for loops to loop through array sq.
      //if the value in sq == numcheck, set found to true

      //After the loop, check to see if numcheck was found 
      //if not found, return false, otherwise set found to false
      //and increment numcheck to be ready to check the next number
    }
    if(found == false)
      return false;
    else
      return true;
  }

  public boolean isMagic()
  {
    int targetNum = 0;
    boolean stuff = false;

    for (int c = 0; c < sq[0].length; c++)
    {
      targetNum += sq[0][c];
    }
    if(column(targetNum) == true && row(targetNum) == true && diagonalLeft(targetNum) == true && diagonalRight(targetNum) == true && fullSquare() == true)
    {
      return true;
    }
    else
      return false;
    //if the rows, columns, diagonals and fullSquare are all
    //true, return true, otherwise, return false.

  }

  public boolean diagonalLeft(int tN)
  {
    int sum = 0;
    //write loop to see if the diagonal from 
    //lower left to upper right is the same as tN
     for(int d = 0; d < sq[0].length; d++)
    {
      sum = sum + sq[d][(sq.length-1) - d];
    }
    return (sum == tN);
  }

  public boolean diagonalRight (int tN)
  {
    int sum = 0;
   //write loop to see if the diagonal from upper left
    //to lower right is the same as tN
    for (int d = 0; d < sq[0].length; d++)
    {
        sum = sum + sq[d][d];
    }

   return (sum == tN);
  }

  public boolean column (int tN)
  {
    boolean same = true;
    int sum = 0;

    //write nested loops to check each column's sum
    //if the sum is not the same as tN, set same to false.
    for(int i = 0; i < sq[0].length; i++)
    {
      for(int j = 0; j < sq[0].length; j++)
      {
        sum = sum + sq[j][i];
      }
    }
    if(sum == tN)
      same = true;
    else
      same = false;

    return same;
  }

  public boolean row(int tN)
  {
   boolean same = true;
    int sum = 0;

    //write nested loops to check each column's sum
    //if the sum is not the same as tN, set same to false.
    for(int i = 0; i < sq[0].length; i++)
    {
      for(int j = 0; j < sq[0].length; j++)
      {
        sum = sum + sq[i][j];
      }
    }
    if(sum == tN)
      same = true;
    else
      same = false;

    return same;
  }

  public String toString()
  {
    String s = "";
    for (int r = 0; r < sq.length; r++)
    {
      for (int c = 0; c < sq[r].length; c++)
      {
        s += sq[r][c] + " ";
      }
      s+= "\n";
    }
    return s;
  }
  }
4

2 に答える 2

2
for(int i = 0; i < sq[0].length; i++)
    {
      for(int j = 0; j < sq[0].length; j++)
      {
        sum = sum + sq[j][i];
      }
    }

外側のループの実行間の合計をリセットしていません。次の列に進む前に、合計を確認してリセットします。

于 2013-01-29T01:21:38.007 に答える
2

あなたが抱えている主な問題は、 for ループの外で合計が同じかどうかをチェックしていることです。私なら次のように書き直します。

public boolean rowAndColumn(int tN)
{
  int rowsum = 0, colsum = 0;
  for(int i = 0; i < sq[0].length; i++)
  {
    for(int j = 0; j < sq[0].length; j++)
    {
      rowsum = rowsum + sq[i][j];
      colsum = colsum + sq[j][i];
    }
    if(rowsum != tN || colsum != tN)
      return false; //no point checking the rest if the sums doesn't match
    rowsum = 0; //reset row count
    colsum = 0; //reset col count
  }
  return true; //if it doesn't return by here, all the sums match
}

編集: fullSquare も意図したとおりに機能しません。次のようなことを試してください:

public boolean fullSquare()
{
  int numcheck = 1;
  boolean found = false;
  while (numcheck < sq.length*sq.length)
  {
    for(int i = 0; i < sq.length;
      for(int j = 0; j < sq.length; j++)
        if(sq[i][j] == numcheck)
          found = true;

    if (!found)
      return false; //if the number wasn't found, it's not a full square
    found = false;
    numcheck++;
  }
    //use nested for loops to loop through array sq.
    //if the value in sq == numcheck, set found to true

    //After the loop, check to see if numcheck was found 
    //if not found, return false, otherwise set found to false
    //and increment numcheck to be ready to check the next number
  return true;
}
于 2013-01-29T01:26:49.567 に答える