【问题标题】:Compare word within words in java在java中比较单词中的单词
【发布时间】:2017-02-15 18:06:33
【问题描述】:

基本上我已经:

public static void main(String[] args)
{
String genre = "Action, Animation, Comdey";
String[] genres   = genre.split(", ");
for(int i=0;i<(genres.length)-1;i++){
    System.out.println(genres[i]);
    if(!genres[i].equals("Action")              || !genres[i].equals("Adventure")
            || !genres[i].equals("Animation")   || !genres[i].equals("Biography")
            || !genres[i].equals("Comedy")      || !genres[i].equals("Crime")
            || !genres[i].equals("Documentary") || !genres[i].equals("Drama")
            || !genres[i].equals("Family")      || !genres[i].equals("Fantasy")
            || !genres[i].equals("Film-Noir")   || !genres[i].equals("Game-Show")
            || !genres[i].equals("History")     || !genres[i].equals("Horror")
            || !genres[i].equals("Music")       || !genres[i].equals("Musical")
            || !genres[i].equals("Mystery")     || !genres[i].equals("News")
            || !genres[i].equals("Reality-TV")  || !genres[i].equals("Sci-Fi")
            || !genres[i].equals("Sport")       || !genres[i].equals("Talk-Show")
            || !genres[i].equals("Thriller")    || !genres[i].equals("War")
            || !genres[i].equals("Western")
            ){
        System.out.println("Selected genres could not be identified");
        return;
   }else {
        System.out.println("Success");
   }
}
}

如您所见,我有一个包含Action, Animation, Comdey 的字符串genre,然后我用,(space) 拆分每个单词,然后使用if() 我比较每个数组。您可能知道我的数组 ActionAnimationComedy 存在于我的 if() 单词列表中。所以不应该执行System.out.println("Selected genres could not be identified");,但它确实执行了,我不知道为什么但这真的吓坏了我。

您可以在https://www.compilejava.net/ 复制/粘贴我的代码并自己查看结果。

如果你能告诉我我做错了什么,我将不胜感激。

【问题讨论】:

    标签: java arrays if-statement


    【解决方案1】:

    如果您尝试检查当前流派是否与可用流派中的none 匹配,则需要将所有 OR (||) 更改为 AND (&amp;&amp;),例如:

    public static void main(String[] args) {
        String genre = "Action, Animation, Comdey";
        String[] genres = genre.split(", ");
        for (int i = 0; i < (genres.length) - 1; i++) {
            System.out.println(genres[i]);
            if (!genres[i].equals("Action") && !genres[i].equals("Adventure") && !genres[i].equals("Animation")
                    && !genres[i].equals("Biography") && !genres[i].equals("Comedy") && !genres[i].equals("Crime")
                    && !genres[i].equals("Documentary") && !genres[i].equals("Drama") && !genres[i].equals("Family")
                    && !genres[i].equals("Fantasy") && !genres[i].equals("Film-Noir") && !genres[i].equals("Game-Show")
                    && !genres[i].equals("History") && !genres[i].equals("Horror") && !genres[i].equals("Music")
                    && !genres[i].equals("Musical") && !genres[i].equals("Mystery") && !genres[i].equals("News")
                    && !genres[i].equals("Reality-TV") && !genres[i].equals("Sci-Fi") && !genres[i].equals("Sport")
                    && !genres[i].equals("Talk-Show") && !genres[i].equals("Thriller") && !genres[i].equals("War")
                    && !genres[i].equals("Western")) {
                System.out.println("Selected genres could not be identified");
                return;
            } else {
                System.out.println("Success");
            }
        }
    }
    

    Here 的逻辑OR 运算符更多。在您的场景中,!genres[i].equals("Action") 评估为 false 表示“操作”,但是,其他条件(如 !genres[i].equals("Adventure"))评估为 true,因此,它会打印“无法识别所选类型”。

    【讨论】:

    • 您不能直接删除! 运算符吗?
    • @Abhijith 尝试删除 ! 并产生与 OP 在问题中提到的输出相同的输出。
    • @darshanMehta 这意味着我浪费了一整天的时间试图找出我做错了什么,无论如何都工作了,谢谢。
    • @DarshanMehta 我的意思是删除 not 运算符 & 切换 if/else 块语句...
    【解决方案2】:

    Stringgenre = "Action, Animation, Comdey" 应改为 Stringgenre = "Action, Animation, Comedy"

    【讨论】:

    • 没关系,我在我的实际字符串中将它作为Comedy,只是拼错了它在这里发布。
    【解决方案3】:

    编写上述代码的更简洁有效的方法是使用 TreeSet:

    public static void main(String[] args)
      {
       String genre = "Action, Animation, Comdey";
        String[] genres = genre.split(", ");
        Set<String> genreSet = new TreeSet<String>();
        genreSet.add("Action");
        genreSet.add("Adventure");
        genreSet.add("Animation");
        genreSet.add("Documentary");
        genreSet.add("Comedy");
        //more genres follows here
    
        for (int i = 0; i < (genres.length) - 1; i++) {
            System.out.println(genres[i]);
            if (!genreSet.contains(genres[i])) {
                System.out.println("Selected genres could not be identified");
                return;
            } else {
                System.out.println("Success");
            }
      }
      }
    

    高效,因为在 TreeSet 中搜索流派字符串的时间复杂度将是 (log n) 的顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2021-05-09
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多