java - 如何在 Java (Eclipse) 中对角检查二维字符数组中的字符串
问题描述
我从用户那里得到一个单词列表作为输入,并将他们的输入存储在 String[] 数组中。然后我创建一个名为 letterGrid 的 char[][] 数组,其中填充了随机字母和用户提供的单词。当 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
一直在尝试理解问题并寻找帮助我解决问题的方法。
解决方案
代码中的问题在于您的 if 条件,checkVertical()
因为行和列可能是第一个或最后一个,但您应该检查行或列。由于Y
在第一行和第二列中找到第一个代码然后继续扫描,您的黄色示例失败,然后继续扫描,最后,它Y
在最后一行中找到并检查rowLocation + i > letterGrid.length - 1 && colLocation + i > letterGrid[0].length
跳过的内容,然后调用else
which 将 1 添加到该行,结果为绑定数组。这应该有效:
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()
推荐阅读
- python-3.x - 在 Mac OSX 上安装 pymssql 和 cython 失败
- javascript - 事件目标随机记录为未定义或预期值
- java - 用Java检查和比较数据库中的数据(时间限制)的好方法是什么?
- sql - 如何将项目添加到 sqlite3 中的单个列表
- java - 如何使用 Netty 从 Java 应用程序提出 GET 查询?
- php - 给定 [API 基本 URL:] 的邮箱中的地址不符合 RFC 2822, 3.6.2。拉拉维尔
- mysql - 用其他列中的值替换列值
- javascript - 按钮不起作用,页面在 POST 方法后继续加载
- c# - 无法将类型“字符串”隐式转换为“System.Collections.Generic.List”
' - php - 如果从数据库中删除用户帐户但用户已登录,则销毁会话