Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java(Eclipse)中对角检查2D字符数组中的字符串_Java_Multidimensional Array_Indexoutofboundsexception_Wordsearch - Fatal编程技术网

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

如何在Java(Eclipse)中对角检查2D字符数组中的字符串,java,multidimensional-array,indexoutofboundsexception,wordsearch,Java,Multidimensional Array,Indexoutofboundsexception,Wordsearch,我从用户那里获取一个单词列表作为输入,并将其输入存储在String[]数组中。然后我创建了一个名为letterGrid的char[][]数组,该数组由随机字母和用户提供的单词填充。然后,用户必须输入他们希望在控制台屏幕上显示letterGrid时查找的单词。然后,程序将检查输入的单词是否水平、垂直或对角出现,并将其打印出s位置。我的checkHorizontal和checkDiagonal方法工作得很好,但是我在checkVertical上遇到了一个问题。例如,我输入的单词示例为红色、橙色、黄色

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

如您所见,当我输入黄色(长度为6个字母)时,程序输出单词的位置,但随后出现越界错误

编辑代码

应@Igor Khvostenkov的请求,以下是代码的其余部分:


私有字符串字;//当用户选择搜索输入的单词时,此变量将是用户的输入
私有int行位置;//此变量将表示单词所在的行号
私有int搭配;//此变量将表示单词所在的列号
//创建一个方法,将用户的单词与字母网格中的元素进行比较
公共空比较(字符串字){
对于(int row=0;row字母网格[0]。长度-1){
回来
}else if(letterGrid[rowLocation][collayout+i]!=word.charAt(i)){
回来
}//结束if..else if
}//循环结束
System.out.println(word+“在第“+rowLocation+”行和第“+Colonment”列水平查找);//word已找到!!
System.out.println();
回来
}//检查结束()
//创建一个方法来检查用户的单词是否垂直出现在字母网格中
公共无效检查垂直(){
对于(int i=1;i<(word.length());i++){
if(rowLocation+i>letterGrid.length-1&&Colonment+i>letterGrid[0].length){
回来
}else if(letterGrid[rowLocation+i][collayout]!=word.charAt(i)){
回来
}//结束if..else if
}//循环结束
System.out.println(word+“在第“+rowLocation+”行和第“+Colonment”列垂直找到);//word已找到!!
System.out.println();
}//checkVertical()结束
//创建一个方法来检查用户的单词是否以对角线形式出现在字母网格中
公共无效检查(){
对于(int i=1;i<(word.length());i++){
if(搭配+i>字母网格[0]。长度-1 | |行位置+i>字母网格。长度-1){
回来
}else if(letterGrid[rowLocation+i][colLocation+i]!=word.charAt(i)){
回来
}//结束if..else if
}//循环结束
System.out.println(word+“在第“+rowLocation+”行和第“+column”列的对角线处找到);//word已找到!!
System.out.println(“”);

}//checkDiagonal()的结尾
代码中的问题在于
checkVertical()中的if条件,即行和列可以是第一个或最后一个,但您应该检查行或列。您的黄色示例失败,原因是第一行和第二列中的第一个代码查找
Y
,然后继续扫描,最后,它在最后一行中查找
Y
,并检查
rowLocation+i>letterGrid.length-1&&collaboration+i>letterGrid[0]。长度已跳过,然后调用
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()
public void checkVertical(){
对于(int i=1;i<(word.length());i++){
if(rowLocation+i>letterGrid.length-1 | |搭配+i>letterGrid[0].length){
回来
}else if(letterGrid[rowLocation+i][collayout]!=word.charAt(i)){
回来
}//结束if..else if
}//循环结束
System.out.println(word+“在第“+rowLocation+”行和第“+Colonment”列垂直找到);//word已找到!!
System.out.println();
}//checkVertical()结束
Its