我的Magic Square代码一直存在问题。它不断地打印出“这是一个神奇的广场”,即使我很肯定它不是。 输入16个整数,然后代码应运行并确定输入的整数是否创建一个魔术方(即所有行,列和对角线的总和都相等)。
我无法弄清楚如何让它打印错误。
import java.util.*;
public class MagicSquare {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner keyboard = new Scanner (System.in);
int [] [] square = new int [4][4];
for (int row = 0; row < 4; row++)
{
for (int col = 0; col < 4; col++)
{
System.out.println("Input value for row " + (row+1) + " column " + (col+1));
square[row][col] = keyboard.nextInt();
}
}
int [] sumRow = new int [4];
int [] sumCol = new int [4];
int sum = 0;
for (int row = 0; row < 4; row ++)
{
for (int col = 0; col < 4; col ++)
{
sum = sum + square[row][col];
sumRow[row] = sum;
}
System.out.println("sum row " + row + "\n" + sumRow[row]);
sum = 0;
}
sum = 0;
for (int col = 0; col < 4; col ++)
{
for (int row = 0; row < 4; row ++)
{
sum = sum + square[row][col];
sumCol[col] = sum;
}
System.out.println("sum columns " + col + "\n" + sumCol[col]);
sum = 0;
}
int [] sumDiag = new int [4];
sum = 0;
for (int row = 0; row < 4; row++)
{
for (int col = 3; col > -1; col--)
{
sum = sum + square [row][col];
sumDiag[row] = sum;
}
System.out.println("sum diagonal " + row + "\n" + sumDiag[row]);
sum = 0;
}
int [] sumDiag2 = new int [4];
sum = 0;
for (int col = 0; col < 4; col ++)
{
for (int row = 3; row > -1; row --)
{
sum = sum + square[row][col];
sumDiag2[col] = sum;
}
System.out.println("sum diagonal 2 " + col + "\n" + sumDiag2[col]);
}
boolean bool = false;
int k = 0; int j = 1;
do
{
if (sumRow[k] == sumRow[j])
{
k = j;
j += 1;
bool = true;
}
else
{
bool = false;
System.out.println("Not a magic square");
break;
}
} while ((k < 4) && (j >- 1));
k = 0; j = 1;
do
{
if (sumCol[k] == sumRow[j])
{
k = j;
j += 1;
bool = true;
}
else
{
bool = false;
System.out.println("Not a magic square");
break;
}
} while ((k < 4) && (j >= -1));
String TorF = "";
if (bool = true)
{
TorF = "is";
}
else if (bool = false)
{
TorF = "is not";
}
System.out.println("This " + TorF + " a magic square.");
}
}
答案 0 :(得分:1)
您的代码中存在许多问题,如评论中所述。
首先,您只需要验证main and secondary diagonals。
其次,比较总和的代码并不适用于所有情况,也不会比较对角线。
另外,if
到底是错的。这样做:
if (bool) {
} else {
}
这是一个解决方案:
int order = square.length;
int[] sumRow = new int[order];
int[] sumCol = new int[order];
int[] sumDiag = new int[2];
Arrays.fill(sumRow, 0);
Arrays.fill(sumCol, 0);
Arrays.fill(sumDiag, 0);
for (int row = 0; row < order; row++) {
for (int col = 0; col < order; col ++) {
sumRow[row] += square[row][col];
}
System.out.println("sum row " + row + "\n" + sumRow[row]);
}
for (int col = 0; col < order; col++) {
for (int row = 0; row < order; row ++) {
sumCol[col] += square[row][col];
}
System.out.println("sum columns " + col + "\n" + sumCol[col]);
}
for (int row = 0; row < order; row++) {
sumDiag[0] += square[row][row];
}
System.out.println("sum diagonal 0 " + "\n" + sumDiag[0]);
for(int row = 0; row < order; row++) {
sumDiag[1] += square[row][order - 1 - row];
}
System.out.println("sum diagonal 1 " + "\n" + sumDiag[1]);
boolean bool = true;
int sum = sumRow[0];
for (int i = 1; i < order; i++) {
bool = bool && (sum == sumRow[i]);
}
for (int i = 0; i < order; i++) {
bool = bool && (sum == sumCol[i]);
}
for (int i = 0; i < 2; i++) {
bool = bool && (sum == sumDiag[i]);
}
String tOrF = "";
if (bool) {
tOrF = "is";
} else {
tOrF = "is not";
}
System.out.println("This " + tOrF + " a magic square.");
此外,您可以采取一些措施来优化此代码。