检查是否是2D数组中的普通幻方

时间:2017-10-12 03:05:28

标签: java arrays

我在这最后一部分代码中遇到了问题。我没有错误,但我没有得到确切的期望输出。

我试图获取第二个2D阵列(square1)的输出以打印出来"是一个普通的魔方#34;但它只会将它识别为一个魔术广场而且我使用的是文本编辑器(SciTE),这让我更难以看到问题所在。我认为这个问题是由于最后一个返回声明,我认为它返回错误,但我不确定如何解决这个问题。我对java很新。任何帮助表示赞赏!

这是似乎遇到问题的代码块:

    public static boolean isNormalMagicSquare(int[][] array) {
    // Checks to see if magic square is a normal magic square with accumulators for accumulated indeces and sum of actual array

        int sum = 0;
        int total = 0;
        // x gives the desired ending length
        int x = array.length * array.length;
        // iterates to find 
        for (int i = 0; i < x; i++) {
            total += i;
            for (int j = 0; j < x; j++) {
                sum = sum + array[i][j];
                if (sum != total) {
                    return false;
                }
            //return true;
            }
        }
    return false;
    }

这是运行代码时得到的输出,该代码适用于第一个2D数组和最后一个2D数组,但不适用于第二个2D数组(square1):

>java -cp . magicSquares
Is not a magic square
Is a magic square
Is a magic square
>Exit code: 0

这是代码块来自的地方(我知道它并不漂亮,但它有效)我们也需要使用我教授所说的所有方法:

public class magicSquares {
public static void main (String[] args) {
    int [][] square = {{1,2,3}, {4,5,6}, {7,8,9}};
    if (isNormalMagicSquare(square)) {
        System.out.println("Is a normal magic square");
    }
    else if (isMagicSquare(square)) {
        System.out.println("Is a magic square");
    }
    else 
        System.out.println("Is not a magic square");



    int [][] square1 = {{2,7,6}, {9,5,1}, {4,3,8}};
    if (isNormalMagicSquare(square1)) {
        System.out.println("Is a normal magic square");
    }
    else if (isMagicSquare(square1)) {
        System.out.println("Is a magic square");
    }
    else 
        System.out.println("Is not a magic square");


    int [][] square2 = {{8,11,14,1}, {13,2,7,12}, {3,16,9,6}, {10,5,4,15}};
    isNormalMagicSquare(square2);

    if (isNormalMagicSquare(square2)) {
        System.out.println("Is a normal magic square");
    }
    else if (isMagicSquare(square2)) {
        System.out.println("Is a magic square");
    }
    else 
        System.out.println("Is not a magic square");

    }


// method that calculates the magic sum number
public static int magicSum(int[][] array) {
    int n = array.length;
    //int M = 0;
    int M = n * (n * n + 1) / 2;
    //System.out.print(M);
return M;
}

public static boolean rowsAreAllEqualToMagicSum(int[][] array) {
    int M = magicSum(array);
    for (int i = 0; i < array.length; i++) {
        int temp = getSumOfRow(array, i);
        if (M != temp) {
            return false;
        }
    }
    return true;
}

public static boolean columnsAreAllEqualToMagicSum(int[][] array) {
    int M = magicSum(array);
    for (int i = 0; i < array.length; i++) {
        int temp = getColumn(array, i);
        if (M != temp) {
            return false;
        }
    }
    return true;
}

public static boolean diagonalsAreBothEqualToMagicSum (int[][] array) {
    int k =magicSum(array);
    if (getSumOfDownDiagonal(array) ==k && getSumOfUpDiagonal(array)==k) {
        return true;
    }
    return false;
}

// method that checks to see if magic sum is equal for all rows, columns, and diagonals
public static boolean isMagicSquare(int[][] array) {
    if (rowsAreAllEqualToMagicSum(array) && columnsAreAllEqualToMagicSum(array) && diagonalsAreBothEqualToMagicSum(array)) {
        return true;
    }
    else 
    return false;
}

public static boolean isNormalMagicSquare(int[][] array) {
    // Checks to see if magic square is a normal magic square with accumulators for accumulated indeces and sum of actual array

        int sum = 0;
        int total = 0;
        // x gives the desired ending length
        int x = array.length * array.length;
        // iterates to find 
        for (int i = 0; i < x; i++) {
            total += i;
            for (int j = 0; j < x; j++) {
                sum = sum + array[i][j];
                if (sum != total) {
                    return false;
                }
            //return true;
            }
        }
    return false;
    }

public static int getSumOfDownDiagonal(int[][] array) {
    //initializes an accumulator and loops through to add the down diagonal
    int total = 0;
    for (int i = 0; i < array.length; i++) {
        total = total + array[i][i];
        //System.out.println(array[i][i]);
    }
    return total;
    //System.out.println(total);
}

public static int getSumOfUpDiagonal(int[][] array) {
    // initializes an accumulator and loops through to add the up diagonal
    int total = 0;
    for (int i = 0; i < array.length; i++) {
            total = total + array[i][array.length - 1 -i];
            //System.out.println(array[i][array.length - 1 -i] + " ");
    }
    return total;
}

public static int getSumOfRow(int[][] array, int index) {
    // accumulates and iterates through to add each row in array
    int total = 0;
    for (int i = 0; i < array.length; i++) {
        total += array[index][i];
    }
    return total;
}

public static int getColumn(int[][] array, int index) {
    // declares a new array and iterates through to add each column
    int[] single = new int[array.length];

    for (int i = 0; i < array.length; i++) {
        single[i] = array[i][index];
    }
    int sum = getSumOfColumn(single);
    return sum;
}

public static int getSumOfColumn(int[] array) {
    // getSumOfColumn method accumulates the specified indexes (column)     
    int total = 0;
    for (int i = 0; i < array.length; i++) {
        total += array[i];
    }
    return total;
}
}

0 个答案:

没有答案