幻方。请检查我的错误

时间:2016-12-01 03:31:29

标签: java

我的任务是在MagicSquare上,我有一些小错误,我相信我无法弄明白。它是一个3x3 magicsquare,随机数为1-9。

请不要更改我们需要使用的方法。

到目前为止我注意到了:

在第76行,我调用方法magicSquare.magicSquare();,因为它要重置,但是当我运行程序时它最终会崩溃。我相信还有更多错误。任何帮助将不胜感激。

        //Scanner part two

        Scanner input = new Scanner(System.in);
        //declare needed variables here
        String userIn = "";

        //this is the start menu
        while(!userIn.equals("start")) {
            System.out.println("Enter 'start' to begin the game.");
            userIn = input.next().toLowerCase();

            if (userIn.equals("start")) {
                    magicSquare.magicSquare();//ultimately starts the magic square method
            }
        }
    }

    //method
    public static void magicSquare() {  
        //initializes the magic square matrix array
        int [][] square = new int[3][3];

        square = resetMatrix(square);//this uses the resetMatrix method to set matrix to all 0
        square = fillMatrix(square);//this uses the fillMatrix method to set the matrix to unique numbers 1-9
        magicSquare.displayMagicSquare(square); //this prints the matrix for user to see
        System.out.println("Rows sum: " +sumRows(square)); //this displays rows sum
        System.out.println("Columns sum: " +sumColumns(square)); //this displays columns sum
        System.out.println("Diagonals sum: " +sumDiagonals(square)); //this displays diagonl sums

        //decides if square is magic using isMagicSquare method
        if (isMagicSquare(square) == true) {

            System.out.println("Matrix is a Magic Square!");

        }
        else if (isMagicSquare(square) == false) {

            System.out.println("Matrix is not a Magic Square. \n");
            resetMatrix(square);// this resets matrix and restarts to find a magic square

            magicSquare.magicSquare();
        }
    }

    //this method to fill matrix with numbers
    public static int[][] fillMatrix(int [][] array) {
        //this loops through each element and uses isUnique method to set random and unique values.
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int num = 0;
                while (isUnique(array, num) == false) {
                    num = (int)(9.0 * Math.random()) + 1;
                }
                array[i][j] = num;
            }
        }
        return array; 
    }

    //this used to reset the matrix by setting each array element to 0
    public static int[][] resetMatrix(int [][] array) {

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                array[i][j] = 0; //this loops through each element in the array and sets them to 0
            }
        }
        return array;
    }

    public static void displayMagicSquare(int[][] array) {

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                  System.out.print(array[i][j] +"   ");//Prints each row for the array
            }
            System.out.println(" ");// this goes to next row (blank)
        }
    }
    /*
     * LOOK HERE
     * EDIT: SumRows () and SumColumns is wrong
     */
    //this is used to calculate the sums of the rows
    public static int sumRows(int [][]array) {// sum of rows and sum of columns is wrong
        int sum = 0;
        int r1=0,r2=0,r3=0;
        for (int i = 0; i < 3; i++) {
            for (int j =0; j< 3; j++) {
                sum += array[i][j];
            }
            if (i==0)
                 r1=sum;//assigns 1st row sum to r1
                if (i==1)
                 r2=sum;//assigns 2nd row sum to r2
                if (i==2)
                 r3=sum;//assigns 3rd row sum to r3
            }
        if (r1==r2&&r2==r3) {//if all rows are equal

            return r1;//return the number (it should be 15)
        }
        else {
            return -1;//returns -1 if row sums are not equal
        }
    }

    /*
     * Fix me
     */

    //this is initially used to calculate the sums of the columns
    public static int sumColumns(int [][]array) {
        int sum = 0;
        for (int j = 0; j < 3; j++) {
            for (int i = 0; i < 3; i++) {
                sum += array[i][j];
            }       
        }
        if (sum/3 !=15) {
            return -1;
        }
        else {
            return sum/3;
        }
    }

    //this is initially used to calculate the sums of the diagonals
    public static int sumDiagonals(int [][] array) {

        int diagSum1 = array[0][0] + array[1][1] + array[2][2];
        int diagSum2 = array[0][2] + array[1][1] + array[2][0];

        if ((diagSum1 == 15) && (diagSum2 == 15)) {
            return 15;
        }
        else {
            return -1;
        }
    }

    //This boolean method goes through the matrix and checks if the value is unique.
    public static boolean isUnique(int [][] array, int num) {

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (array[i][j] == num) {
                    //Check whether IF  an element is equal to the number assigned it 
                    //IT returns false requiring the variable to be changed!
                    return false; 

                }
            }
        }
        return true;        
    }

    public static boolean isMagicSquare(int [][] array) {

        //as a result, this fundamentally checks if the matrix/array is a MagicSquare
        if ((+sumRows(array) == 15) && (+sumColumns(array) == 15) && (+sumDiagonals(array) == 15)) {
            return true;
        }
        else {
            return false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

<强>更新

除了发生递归之外,我几乎修复了所有内容。

就在这一部分:

if (isMagicSquare(square) == true) {

    System.out.println("Matrix is a Magic Square!");
}
else if (isMagicSquare(square) == false) {

    System.out.println("Matrix is not a Magic Square. \n");
    resetMatrix(square); // this resets matrix and restarts to find a magic square

    magicSquare.magicSquare();
}

magicSquare.magicSquare();导致相同错误的有限行,但程序运行正常?帮助