Я получаю список слов в качестве ввода от пользователя и сохраняю их ввод в массиве String [].Затем я создаю массив char [] [] с именем letterGrid, который заполнен случайными буквами и словами, предоставленными пользователем.Затем пользователь должен ввести слово, которое он хочет найти, когда на экране консоли отображается letterGrid.Затем программа проверит, появляется ли введенное слово по горизонтали, вертикали или диагонали, и распечатает его s location. I have the checkHorizontal and checkDiagonal methods working fine, but I am having a problem at checkVertical. For example, my sample input of words was
красный, оранжевый, желтый, зеленый, синий, фиолетовый, радуга , and
цвета.выходная сетка:
Как вы можете видеть, когда я набираю желтый (длина 6 букв), программа выводит местоположение слова,но тогда есть ошибка вне границы.
РЕДАКТИРОВАННЫЙ КОД
Вот остаток кода по запросу @Igor Khvostenkov:
private String word; // This variable will be the user`s input when they chose to search for a word they have entered
private int rowLocation; // This variable will represent the row number in which the word is at
private int colLocation; // This variable will represent the column number in which the word is at
// Create a method to compare the user`s word to the elements in the letter grid
public void compare (String word) {
for (int row = 0; row < letterGrid.length - 1; row++) {
for (int col = 0; col < letterGrid[row].length - 1; col++) {
if (letterGrid[row][col] == word.charAt(0)) {
rowLocation = row;
colLocation = col;
wordContains(); // Call on method to see if the word entered by the user appears horizontally, vertically, or diagonally
}//end of if
}//end of inner for loop
}//end of outer for loop
}//end of compare(word)
// Create a method that will check the direction of the user`s word input
public void wordContains() {
checkHorizontal(); // Checking if the word appears horizontally
checkVertical(); // Checking id the word appears vertically
checkDiagonal(); // Checking if the word appears diagonally
}//end of wordContains()
// Create a method to check if the user`s word appears HORIZONTALLY in the letter grid
public void checkHorizontal() {
for (int i = 1; i < (word.length()); i++) {
if (colLocation + i > letterGrid[0].length - 1) {
return;
} else if(letterGrid[rowLocation][colLocation + i] != word.charAt(i)) {
return;
}//end of if..else if
}//end of for loop
System.out.println(word + " found horizontally at row " + rowLocation + " and column " + colLocation); // Word found!!
System.out.println();
return;
}//end of checkHorizontal()
// Create a method to check if the user`s word appears VERTICALLY in the letter grid
public void checkVertical() {
for (int i = 1; i < (word.length()); i++) {
if (rowLocation + i > letterGrid.length - 1 && colLocation + i > letterGrid[0].length) {
return;
} else if (letterGrid[rowLocation + i][colLocation] != word.charAt(i)) {
return;
}//end of if..else if
}//end of for loop
System.out.println(word + " found vertically at row " + rowLocation + " and column " + colLocation); // Word found!!
System.out.println();
}//end of checkVertical()
// Create a method to check if the user`s word appears DIAGONALLY in the letter grid
public void checkDiagonal() {
for (int i = 1; i < (word.length()); i++) {
if (colLocation + i > letterGrid[0].length - 1 || rowLocation + i > letterGrid.length - 1) {
return;
} else if (letterGrid[rowLocation + i][colLocation + i] != word.charAt(i)) {
return;
}//end of if..else if
}//end of for loop
System.out.println(word + " found diagonally at row " + rowLocation + " and column " + colLocation); // Word found!!
System.out.println("");
}//end of checkDiagonal()
Я могу t seem to know why this is happening, and how I can fix this. I am not that familiar with ArrayIndexOutofBounds Exceptions as I barely go through them, but recently I have been and I
пытаться понять проблему и искать способы помочь мне решить ее.