繁体   English   中英

如何用Java对角检查2D Char数组中的字符串(Eclipse)

[英]How to Diagonally Check String in a 2D Char Array in 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 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 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 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一直在尝试理解问题并寻找帮助我解决问题的方法。

代码中的问题是在checkVertical()中的if条件下,行和列可以是第一个或最后一个,但是您应该检查行或列。 您的黄色示例失败,因为第一个代码在第一行和第二列中找到Y ,然后继续扫描,最后在最后一行中找到Y ,并检查rowLocation + i > letterGrid.length - 1 && colLocation + i > letterGrid[0].length跳过,然后调用else将行加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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM