有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当比较单词和文本时,java帮助修复true或false条件

我将文本拆分成单词,并希望将其与字符串进行比较,然后如果在文本中找到单词,则将假值返回真值。我的问题是,当使用equals()方法进行检查时,它总是返回false。说信息不在文本中。我该怎么解决这个问题

      String test = "Today will go to rainning";
    String words = "go";

    String[] arrayWords = test.split(" ");
    boolean verify = true;
    for (int i = 0; i < arrayWords.length; i++) 

       if (arrayWords[i].equals(words)) {
           verify = true;                 
            System.out.println("This word find in text");            
            break;
        } else {
             verify = false;
            System.out.println("This word not find in text.");
            break;
        }
    }

系统。出来println(“这个词在文本中找不到”)


共 (1) 个答案

  1. # 1 楼答案

    break in else needs to be removed
    verify = false;
    System.out.println("This word not find in text.");
    break;
    since after first comparison it will come out of loop with false printed.
    keep a boolean variable and set it to true in if block and print the 
    answer based on that and we can keep the break in if