有 Java 编程相关的问题?

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

java完成一些搜索逻辑

请帮我添加一些代码来完成一个逻辑。我有一个some搜索方法(publicstringsearch(stringurl,stringsomeword))通过jsp标题在内容中查找单词。所以这里有一个代码

    String[] argi = {"http://www.google.com", "http://www.youtube.com"};

         for (int i = 0; i < argi.length; i++) {

            String result = search(argi[i], "\\b"+word+"\\b");

              if (word == null || word.isEmpty()) {

                        str = "Enter a search word!";
            }
                 else if(!word.matches("^\\w+$")){

                       str="Incorrect word!";
            }

                 else if (result != null) {

                        str += "Search phrase " + "<b>"+ word + "</b>" + " have found in " + "<a href=\"" + argi[i] + "\">" + result + "</a>"+ "<p></p>";

        }
else if  (????????) {str="Word not found!";}
        }
}

如果要获得“str=”找不到单词,我应该在最后一个“中输入什么“如果我输入搜索,请输入一些内容中缺少的正常单词或数字


共 (2) 个答案

  1. # 1 楼答案

    这个街区

    else if  (????????) {str="Word not found!";}  //Remove this line
    

    应该是这样的

    将此代码移出for循环

    for (int i = 0; i < argi.length; i++){
    }
    str="Word not found!";
    
  2. # 2 楼答案

    不需要添加其他条件。但是,您的代码可以这样编写,因为您不需要将关于word的if语句放在for循环中:

    String[] argi = {"http://www.google.com", "http://www.youtube.com"};
    String str = "";
    
    if (word == null || word.isEmpty()) {
        str = "Enter a search word!";
    } 
    else if(!word.matches("^\\w+$")) {
        str = "Incorrect word!";
    }
    else {
        for (int i = 0; i < argi.length; i++) {
            String result = search(argi[i], "\\b" + word + "\\b");
            if (result != null) {
                str += "Search word <b>" + word + "</b> have been found in "
                    + "<a href=\"" + argi[i] + "\">" + result + "</a><br>";
            }
        }
        if (str.isEmpty()) {
            str = "Word not found!";
        }
    }