我如何通过更改if语句来缩短代码,以及如何解决无法修复的代码错误

问题描述 投票:-3回答:1

我想知道如何缩短我的代码并修复它,因为我现在还不太擅长Java,如果有人可以帮忙,谢谢


    String emotion [] = new String [10];
    emotion [0] = "Happy";
    emotion [1] = "Sad";
    emotion [2] = "Vibing";
    emotion [3] = "Trapping";  
    emotion [4] = "Ambationz";
    emotion [5] = "Anxious";
    emotion [6] = "Positive";
    emotion [7] = "Scared"; 
    emotion [8] = "Worried"; 
    emotion [9] = "Focused";
    for(int i = 0; i <  emotion.length; i++){
      System.out.println(emotion[i]);
    }

  //My Method for finding the user input and matching it with their song 
   public static String songfinder( String w ) 
    {
      if(w.equals("Happy"));
        return "https://www.youtube.com/watch?v=ZbZSe6N_BXs";
      (w.equals("happy"));
        return "https://www.youtube.com/watch?v=ZbZSe6N_BXs";
      if(w.equals("Sad"));
       return " https://www.youtube.com/watch?v=pgN-vvVVxMA"; 
      if(w.equals("sad"));
        return " https://www.youtube.com/watch?v=pgN-vvVVxMA";
      if(w.equals("Vibing"));
        return "https://www.youtube.com/watch?v=e2qG5uwDCW4";
     if(w.equals("vibing"));
        return "https://www.youtube.com/watch?v=e2qG5uwDCW4";  
      if(w.equals("Trapping"));
        return "https://www.youtube.com/watch?v=XuEx6lNHZjM";
        if(w.equals("trapping"));
       return"https://www.youtube.com/watch?v=XuEx6lNHZjM";
       if(w.equals("Ambationz"));
         return "https://www.youtube.com/watch?v=cQZqPi1aHNo";
       if(w.equals("ambationz"));
       return "https://www.youtube.com/watch?v=cQZqPi1aHNo";
       if(w.equals("Positive"));
       return "https://www.youtube.com/watch?v=h4UqMyldS7Q";
       if(w.equals("positive"));
       return"https://www.youtube.com/watch?v=h4UqMyldS7Q";  
       if(w.equals("Scared"));
         return"https://www.youtube.com/watch?v=CtdsvvMjJ0I";
       if(w.equals("scared"));
       return"https://www.youtube.com/watch?v=CtdsvvMjJ0I";
       if(w.equals("Worried"));
       return "https://www.youtube.com/watch?v=L3HQMbQAWRc";
       if(w.equals("worried"));
       return"https://www.youtube.com/watch?v=L3HQMbQAWRc";
       if(w.equals("Focused"));
         return"https://www.youtube.com/watch?v=hHW1oY26kxQ";
      if(w.equals("focused"));
       return"https://www.youtube.com/watch?v=hHW1oY26kxQ";
      }
}

im接受用户输入并使用与我的字符串数组中相同的单词匹配用户答案,但是有一种方法可以执行相同的操作,但是没有if语句

java
1个回答
0
投票

您可以消除所有的if语句,在这种情况下使用Map,如下所示:

public class Songs {

    private static final String url = "https://www.youtube.com/watch?v=";
    private static final Map<String, String> emotionSongMap = new HashMap<>();
    static{
        emotionSongMap.put("sappy", "ZbZSe6N_BXs");
        emotionSongMap.put("sad", "pgN-vvVVxMA");
    }

    public static String findsong(String emotion) {
        String songKey = emotionSongMap.get(emotion.toLowerCase());
        if (songKey == null)
        {
            throw new IllegalArgumentException(emotion + "...?");
        }

        return url + songKey;
    }
}

您可以在情感列表中进行这样的迭代:songTypeUrlMap.keySet()(确定,精确地设置)

© www.soinside.com 2019 - 2024. All rights reserved.