Testing a .txt file for a Magic Square Java

后端 未结 2 772
一向
一向 2020-12-21 11:46

I didn\'t want to have to ask, but I can not figure out this assignment, and neither could the TA when I asked for help.

I have to take input from a text file, feed

2条回答
  •  有刺的猬
    2020-12-21 12:00

    You never change row and col in your for-loop which is a bug. For easier understanding, I suggest using two nested for-loops over rowIndex and colIndex and using a little helper to get the value from the 1-D Array like so:

    int getValue(int row, int col){
        return magicSquare.get( row * _n + col );
    }
    

    Assuming the 1-D Array is a "flattened" square that is built up like (0,0), (0,1), ... (0,_n), (1, 0) , ... (_n,_n)

    To make it clearer: Iterate over the square instead of using the index:

    for( int row = 0 ; row < _n ; row++){
        for( int col = 0 ; col < _n ; col++){
           // Your stuff here.
        }
    }
    

提交回复
热议问题