魔方程序帮助(Java) [英] Magic Square program help (Java)

查看:191
本文介绍了魔方程序帮助(Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的AP计算机科学类处理二维数组的作业。基本上,程序所做的是将数值输入到一个幻方(所有的行,列和对角线加起来是相同的数字),并确定它是否是一个幻方。当我们的算法检查它是否是Magic时,我的算法不断地发出false(根据isMagic()方法)。我相信我的row()和column()方法有问题,如果有人可以给我一个很好的解决方案。 c> public class Square
{
private int [] [] sq;
private int position = 0;

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

$ b $ **
*在给定的位置上增加矩阵的值row,col
* /

public void add(int value,int row,int col)
{
sq [row] [col] = value;
//在这里填写代码。


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

//使用嵌套for循环循环数组sq
//如果sq == numcheck中的值发现为真

// //循环,检查numcheck是否被找到
//如果找不到,返回false,否则设置为false
//增加numcheck准备检查下一个数字
}
if(found == false)
return false;
else
返回true;


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

for(int c = 0; c< sq [0] .length; c ++)
{
targetNum + = sq [0] [c] (targetNum)== true&& row(targetNum)== true&&&; diagonalLeft(targetNum)== true&&& diagonalRight(targetNum)==(
) true&& fullSquare()== true)
{
return true;
}
else
return false;
//如果行,列,对角线和fullSquare都是
// true,则返回true,否则返回false。


$ b $ public boolean diagonalLeft(int tN)
{
int sum = 0;
//写循环来查看从
//左下到右上的对角线是否与tN
相同(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;
//写循环来查看从左上角
//到右下角的对角线是否与tN
相同(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;

//编写嵌套循环来检查每列的总和
//如果总和不等于tN,则将其设置为false。 (int j = 0; i< sq [0] .length; i ++)

{
sum = sum + sq [j] [i];


if(sum == tN)
same = true;
else
same = false;

返回相同;


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

//编写嵌套循环来检查每列的总和
//如果总和不等于tN,则将其设置为false。 (int j = 0; i< sq [0] .length; i ++)

{
sum = sum + sq [i] [j];


if(sum == tN)
same = true;
else
same = false;

返回相同;


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


$ / code $ / pre

解决方案

你遇到的主要问题是你正在检查和循环之外的总和是否相同。我会重新写这样的:

$ p $ public boolean rowAndColumn(int tN)
{
int rowsum = 0,colsum = 0; (int j = 0; i< sq [0] .length; i ++)

{
rowsum = rowsum + sq [i] [j];
colsum = colsum + sq [j] [i];

if(rowsum!= tN || colsum!= tN)
return false; //如果总和不匹配,则不检查其余部分
rowsum = 0; //重置行数
colsum = 0; //重置列数
}
返回true; //如果没有在这里返回,所有的总和匹配
}

编辑:fullSquare也不会按预期工作。试试像这样:

  public boolean fullSquare()
{
int numcheck = 1;
boolean found = false; (int i = 0; i for(int j = 0)
while(numcheck {
($!);
找到=真;

if(!found)
返回false; //如果找不到数字,它不是一个完整的方形
found = false;
numcheck ++;
}
//使用嵌套for循环循环数组sq
//如果sq == numcheck中的值被设置为true
$ b $ // //在循环之后,检查numcheck是否被找到
/ /如果没有找到,返回false,否则设置发现为false
//增加numcheck准备检查下一个数字
返回true;
}


This is homework for my AP Computer Science class dealing with 2D Arrays. Basically what the program does is input values into a Magic Square (all rows, columns, and diagonals add up to be the same number) and determines if it is a Magic Square. My algorithm continually puts out false (according to the isMagic() method) for when it checks if it's Magic. I believe I have problems in my row() and column() methods and if anyone could provide me a solution that would be great.

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;
  }
  }

解决方案

The main problem you're having is you are checking if the sum is the same outside the for loop. I'd re-write it like this:

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
}

Edit: fullSquare also won't work as intended. Try something like this:

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;
}

这篇关于魔方程序帮助(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆