我正在为我的编程类编写代码,如果在2层多维数组中以对角线,垂直或水平方式在一行中具有4个相等的值,则应该对它进行读取和罚款。这是我的代码。
public static boolean isConseccutiveFour (int[][] matrix){
//creates a boolean to give answer later on
boolean connection = true;
int[][] consecFour = matrix;
//incrementing for loops that move through the two arrays by going horizontally then diagonally
for (int Y = 0; Y < consecFour.length - 3; Y++){
for(int X= 0; X < consecFour[0].length - 3; X++){
//if statement used to give the proper constraints for diagonal check
if ((consecFour.length - Y < 3) && (consecFour[0].length - X < 3)){
if ( consecFour[X][Y] == consecFour[X + 1][Y + 1] && consecFour[X][Y] == consecFour[X+2][Y+2] && consecFour[X][Y] == consecFour[X+3][Y+3])
connection = true;
}
//if statement used to give the proper constraints for diagonal check
else if ((consecFour.length - Y < 3) && (consecFour[0].length < 3)){
if ( consecFour[X][Y] == consecFour[X-1][Y-1] && consecFour[X][Y] == consecFour[X-2][Y-2] && consecFour[X][Y] == consecFour[X-3][Y-3])
connection = true;
}
//if statement used to give the proper constraints for horizontal check
else if (consecFour[0].length - X < 3){
if(consecFour[X][Y] == consecFour[X+1][Y] && consecFour[X][Y] == consecFour[X+2][Y] && consecFour[X][Y] == consecFour[X+3][Y])
connection = true;
}
//if statement used to give the proper constraints for vertical check
else if (consecFour.length - Y < 3){
if ( consecFour[X][Y] == consecFour[X][Y + 1] && consecFour[X][Y] == consecFour[X][Y+2] && consecFour[X][Y] == consecFour[X][Y+3])
connection = true;
}
}
}
//return statement of boolean value
return connection;
我当前的问题是,无论放入什么数组,它总是返回true,我知道这似乎是一个愚蠢的错误,但是我真的找不到错误所在。在此方法称为检查之前,我确实在我的主要声明中确实要确保输入数组的长度大于4,宽度大于4。这已经在Java中了,您已经知道,答案将不胜感激。
最佳答案
错误是您永远不要将连接设置为false,只添加最后一个,使连接等于false或使连接为false,默认情况下不为true。
public static boolean isConseccutiveFour (int[][] matrix){
//creates a boolean to give answer later on
boolean connection = false;
int[][] consecFour = matrix;
//incrementing for loops that move through the two arrays by going horizontally then diagonally
for (int Y = 0; Y < consecFour.length - 3; Y++){
for(int X= 0; X < consecFour[0].length - 3; X++){
//if statement used to give the proper constraints for diagonal check
if ((consecFour.length - Y < 3) && (consecFour[0].length - X < 3)){
if ( consecFour[X][Y] == consecFour[X + 1][Y + 1] && consecFour[X][Y] == consecFour[X+2][Y+2] && consecFour[X][Y] == consecFour[X+3][Y+3])
connection = true;
}
//if statement used to give the proper constraints for diagonal check
else if ((consecFour.length - Y < 3) && (consecFour[0].length < 3)){
if ( consecFour[X][Y] == consecFour[X-1][Y-1] && consecFour[X][Y] == consecFour[X-2][Y-2] && consecFour[X][Y] == consecFour[X-3][Y-3])
connection = true;
}
//if statement used to give the proper constraints for horizontal check
else if (consecFour[0].length - X < 3){
if(consecFour[X][Y] == consecFour[X+1][Y] && consecFour[X][Y] == consecFour[X+2][Y] && consecFour[X][Y] == consecFour[X+3][Y])
connection = true;
}
//if statement used to give the proper constraints for vertical check
else if (consecFour.length - Y < 3){
if ( consecFour[X][Y] == consecFour[X][Y + 1] && consecFour[X][Y] == consecFour[X][Y+2] && consecFour[X][Y] == consecFour[X][Y+3])
connection = true;
}
}
}
//return statement of boolean value
return connection;