gpt4 book ai didi

java - Magic Square 程序帮助 (Java)

转载 作者:行者123 更新时间:2023-12-01 18:54:27 25 4
gpt4 key购买 nike

这是我处理 2D 数组的 AP 计算机科学类(class)的作业。基本上,该程序的作用是将值输入到幻方中(所有行、列和对角线加起来都是相同的数字)并确定它是否是幻方。当我的算法检查它是否是 Magic 时,它会不断地输出 false(根据 isMagic() 方法)。我相信我的 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;
}
}

最佳答案

您遇到的主要问题是您正在检查 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;
}

关于java - Magic Square 程序帮助 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com