java - 如何在 Java 中对角检查 2D 字符数组中的字符串 (Eclipse)

标签 java multidimensional-array indexoutofboundsexception wordsearch

我正在获取用户输入的单词列表,并将其输入存储在 String[] 数组中。然后,我创建一个名为 letterGrid 的 char[][] 数组,其中填充了随机字母和用户提供的单词。当 letterGrid 显示在控制台屏幕上时,用户必须输入他们想要查找的单词。然后程序将检查输入的单词是否水平、垂直或对角显示,并打印出它的位置。我的 checkHorizo​​ntal 和 checkDiagonal 方法工作正常,但我在 checkVertical 时遇到问题。例如,我的单词示例输入是红色、橙色、黄色、绿色、蓝色、紫色、彩虹颜色。下面是输出网格:

Word Search Error - Console Screen

如您所见,当我输入黄色(长度为 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()

似乎不知道为什么会发生这种情况,以及如何解决这个问题。我对 ArrayIndexOutofBounds 异常不太熟悉,因为我几乎没有经历过它们,但最近我一直在尝试理解这个问题并寻找帮助我解决它的方法。

最佳答案

代码中的问题在于 checkVertical() 中的 if 条件,行和列可以是第一个或最后一个,但您应该检查行或列。您的黄色示例失败,因为第一个代码在第一行和第二列中找到 Y ,然后继续扫描,最后,它在最后一行找到 Y 并检查 rowLocation + i > letterGrid.length - 1 && colLocation + i > letterGrid[0].length 跳过,然后调用 else 向该行添加 1,结果输出绑定(bind)数组。这应该有效:

 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()

关于java - 如何在 Java 中对角检查 2D 字符数组中的字符串 (Eclipse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54312474/

相关文章:

python - 使用 numpy.mean 或 numpy.average 平均二维 numpy.array

java - 在 Oreo 上使用 EditText 单击 FAB 时出现 IndexOutOfBoundsException

java - 在 WSL 和 Windows 上安装 SDK(JDK、Maven、Gradle)?

ruby - 检查多维数组中子数组大小是否相等的最优雅的方法?

javascript - 检查多维对象是否为空

java - 如何检查 vector vector 中的位置是否超出范围?

java - 我的 squaretest 也没有分析其他大小和 ArrayIndexOutofBoundsException

java - Apache POI 设置单元格字体和字体颜色

java - 如何在 Eclipse 插件中检索当前文件的编程语言?

java - 从 JSON 生成 Java 对象