Java:即使条件为false,也要执行while循环

Java:即使条件为false,也要执行while循环,java,loops,multidimensional-array,do-while,Java,Loops,Multidimensional Array,Do While,这是一个单词搜索程序。它搜索的文本被输入并在另一个类中转换为二维数组 这是程序正在搜索的文本: 10 //rows 15 //columns fqexfecmxdvjlgu cxomfslieyitqtz nucatfakuxofegk hfytpnsdlhcorey pgrhdqsypyscped ckadhyudtioapje yerjodxnqzztfmf hypmmgoronkzhuo hdskymmpkzokaao amuewqvtmrlglad 出于某种原因,即使键入了终止字符串e

这是一个单词搜索程序。它搜索的文本被输入并在另一个类中转换为二维数组

这是程序正在搜索的文本:

10 //rows
15 //columns
fqexfecmxdvjlgu
cxomfslieyitqtz
nucatfakuxofegk
hfytpnsdlhcorey
pgrhdqsypyscped
ckadhyudtioapje
yerjodxnqzztfmf
hypmmgoronkzhuo
hdskymmpkzokaao
amuewqvtmrlglad
出于某种原因,即使键入了终止字符串
end
,它也总是进入我的
checkDown()
方法并创建越界错误。如果我注释掉该方法,然后只执行
checkRight()
checkDiagonal()
方法,一切似乎都正常。 这是我的代码:

import java.util.Scanner;

public class WordSearch
{
    private char[][] array;
    private String targetWord;
    private int rowLocation;
    private int colLocation;

    public WordSearch(char[][] inArray)
    {
        array = inArray;
    }

    public void play()
    {
        do{
            for (int row = 0; row < array.length; row++)
            {
                for (int col = 0; col < array[row].length; col++)
                {
                    System.out.print(array[row][col]);
                }
                System.out.println();
            }

            System.out.println();
            Scanner input = new Scanner(System.in); 
            System.out.println("What word would you like to search for? Type end to quit: ");
            targetWord = input.nextLine();
            System.out.println("Typed in: " + targetWord);
            System.out.println();

            compareFirst(targetWord);
        } while (!targetWord.equals("end"));

    }

    public void compareFirst(String inWord)
    {
        for (int row = 0; row < array.length; row++)
        {
            for (int col = 0; col < array[row].length; col++)
            {
                if(array[row][col] == inWord.charAt(0))
                {

                    rowLocation = row;
                    colLocation = col;

                    suspectAnalysis();
                }
            }
        }
    }

    public void suspectAnalysis()
    {
        checkRight();
        checkDown();
        checkDiagonal();
    }


    public void checkRight()
    {
        for(int i = 1; i < (targetWord.length()); i++)
        {
            if(colLocation + i > array[0].length - 1)
            {
                return;
            }

            else if(array[rowLocation][colLocation + i] != targetWord.charAt(i))
            {
               return;
            }
        }
        System.out.println(targetWord + " found horizontally at row " + rowLocation + " and column " + colLocation);
        System.out.println();

        return;

    }


    public void checkDown()
    {
        for(int i = 1; i < (targetWord.length()); i++)
        {
            if(rowLocation + i > array.length - 1 && colLocation + i > array[0].length - 1)
            {
                return;
            }
            else if(array[rowLocation + i][colLocation] != targetWord.charAt(i))
            {
                return;
            }
        }
        System.out.println(targetWord + " found vertically at row " + rowLocation + " and column " + colLocation);
        System.out.println();          
    }

    public void checkDiagonal()
    {
        for(int i = 1; i < (targetWord.length()); i++)
        {
            if(colLocation + i > array[0].length - 1 || rowLocation + i > array.length - 1)
            {
                return;
            }

            else if(array[rowLocation + i][colLocation + i] != targetWord.charAt(i))
            {
                return;
            }
        }
        System.out.println(targetWord + " found diagonally at row " + rowLocation + " and column " + colLocation);
        System.out.println();
    }
}
import java.util.Scanner;
公共类词汇搜索
{
私有字符[][]数组;
专用字符串targetWord;
私人位置;
私人智力搭配;
公共词搜索(char[][]inArray)
{
数组=数组;
}
公共游戏
{
做{
for(int row=0;row数组[0]。长度-1)
{
回来
}
else if(数组[rowLocation][并置+i]!=targetWord.charAt(i))
{
回来
}
}
System.out.println(targetWord+“在行“+行位置+”和列“+并置处水平找到);
System.out.println();
回来
}
公共无效检查()
{
对于(int i=1;i<(targetWord.length());i++)
{
if(rowLocation+i>array.length-1&&colLocation+i>array[0].length-1)
{
回来
}
else if(数组[行位置+i][并置]!=targetWord.charAt(i))
{
回来
}
}
System.out.println(targetWord+“垂直位于行“+行位置+”和列“+并置处);
System.out.println();
}
公共无效检查()
{
对于(int i=1;i<(targetWord.length());i++)
{
if(并置+i>array[0].length-1 | | rowLocation+i>array.length-1)
{
回来
}
else if(数组[rowLocation+i][consolution+i]!=targetWord.charAt(i))
{
回来
}
}
System.out.println(targetWord+“位于行“+行位置+”和列“+并置的对角位置);
System.out.println();
}
}
为什么在注释掉
checkDown()
方法时不会发生这种情况?我怎样才能修好它


我非常感谢你的帮助。谢谢大家!

有三种情况会导致失败:

  • 因为你使用do while循环,它在你的循环结束时检查他的状态,程序正在寻找“结束之前结束”这个词

  • 你的最后一行有一个“e”(这就是你的运气;-)。所以你的分析开始了 第9行,第3列

  • 这种情况:

    else if(array[rowLocation + i][colLocation] != targetWord.charAt(i))
    
    if(rowLocation+i>array.length-1&&colLocation+i>array[0].length-1)

  • aka
    if(9+1>9&&3+1>14)

    返回true&&false=>false 在您的下一种情况下导致弹跳失效:

    else if(array[rowLocation + i][colLocation] != targetWord.charAt(i))
    
    阿卡

    因此,将&&更改为| |

    如果你不想检查“结束”这个词,就用“确实”和“通过”来交换你的do while

    if(!targetword.equals("end"))
        break;
    

    扫描后立即检查。

    检查将索引移出绑定异常的语句。这种异常不难理解和修复!@Bluasul您可以添加用于测试WordSearch的代码吗,WordSearch构造函数的示例参数是什么?您的问题是否简单到知道
    do…while
    循环将始终在测试
    while
    表达式中的条件之前执行循环体?也就是说,您真的打算使用
    while
    循环(在决定是否执行循环体之前测试条件)?@alainlompo它从文件中读取数据,我将把它添加到我的问题中