如何调出歌曲的Id进行比较

如何调出歌曲的Id进行比较

http://img1.sycdn.imooc.com//climg/5c962b2c0001332111480672.jpg

1
<br>

我在测试时,发现可以重复向主播放列表添加id相同的歌曲,我想在输入id后,能够进行判断主播放列表中有没有相同的id歌曲。有的话提示,没有继续添加。但我不会编。

1
<br>


正在回答

登陆购买课程后可参与讨论,去登陆

4回答

老师用你贴的代码测试添加重复数据是不能添加的,你如果能正常添加查看一下你的Song类的equals()方法是否正确。可以把equals()单独贴出来

http://img1.sycdn.imooc.com//climg/5c9b49aa000152e207980770.jpg

  • 士林健津_007 提问者 #1
    老师,我的意思是在只有歌曲 ID相同,歌曲名称和演唱者不同的情况下,能不能做到说歌曲id已经存在,请不要添加之类的,因为从实际情况来说,歌曲ID是唯一的,但有时歌曲名有重复的,演唱者不同的情况。
    2019-03-27 20:40:10
  • irista23 回复 提问者 士林健津_007 #2
    你想只比较歌曲id或者id歌曲名演唱者都相同,这个是由你Song类中重写的equals()方法决定的,你想只比较歌曲id也可以,那就把equals()比较的歌曲名演唱者去掉就可以了。
    2019-03-28 10:49:37
  • 士林健津_007 提问者 回复 irista23 #3
    就是这么回事,达到预想的功能了。谢谢
    2019-03-28 20:16:15
提问者 士林健津_007 2019-03-27 11:09:25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    public void addToPlayList(Song song){
        //排除重复添加的情况
        boolean flag=false;
        for(Song song1:musicList){
            if(song1.equals(song)){
                flag=true;
                break;
            }
        }
        if(flag){
            System.out.println("该歌曲已经存在。请不要重复添加。");
        }else{
            musicList.add(song);
        }
    }
     
这是涉及问题的方法
 
public void mainProcess(){
        stdIn = new Scanner(System.in);
        PlayListCollection plc=new PlayListCollection();  //创建播放列表容器(播放器)
        PlayList mainplayList=new PlayList("主播放列表");
        PlayList playList1=null;
        plc.addPlayList(mainplayList);
        Test test=new Test();
        int w=0;
        int e=0;
        int q=0;
        BigOuter:
        while(true){
        test.mainMenu();
        System.out.println("请输入对应数字进行操作:");
        Outer:
            while(true){
                try{
        q=stdIn.nextInt();
        }catch(InputMismatchException p){
            stdIn.next();
            System.out.println("您的输入有误,请重新输入:");
            continue Outer;
        }
        if(q==0){
            break BigOuter;
        }
        switch(q){
        case 1:
              while(true){
              test.playListManage();
              System.out.println("请输入对应的数字对播放列表进行管理:");
              int number1=0;
              int str2=0;
              Ainner:
              while(true){
              try{
              w=stdIn.nextInt();
              }catch(InputMismatchException u){
                  stdIn.next();
                  System.out.println("您的输入有误,请重新输入:");
                  continue Ainner;
              }
              if(w==9){
                  break Outer;
              }switch(w){
              case 1:System.out.println("将歌曲添加到主播放列表");
                     System.out.println("请输入要添加的歌曲数量:");
                     Cinner:
                     while(true){
                     try{
                     number1=stdIn.nextInt();
                     }catch(InputMismatchException h){
                         stdIn.next();
                         System.out.println("您的输入有误,请重新输入:");
                         continue Cinner;
                     }
                     for(int i=1;i<=number1;i++){
                         System.out.println("请输入第"+i+"首歌曲:");
                         System.out.println("请输入要添加的歌曲的id:");
                         String str11=stdIn.next();
                         System.out.println("请输入要添加的歌曲名称:");
                         String str12=stdIn.next();
                         System.out.println("请输入演唱者:");
                         String str13=stdIn.next();
                         Song song1=new Song(str11,str12,str13);
                         mainplayList.addToPlayList(song1);
                     }
                     System.out.println("添加完成。");
                     System.out.println();
                     break Ainner;
                     }
这是涉及问题的测试方法
请老师帮我看看


irista23 2019-03-25 16:43:05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void addToPlayList(Song song) {
   // 排除重复添加
    boolean flag = false;// 判断播放列表中的歌曲是否存在
    for (Song song1 : musicList) {
        if (song1.equals(song)) {
            flag = true;
            break;
        }
     }
     if (flag) {
         System.out.println("该歌曲已经存在于播放列表中,添加失败!");
     else {
         musicList.add(song);
     }
}


  • 提问者 士林健津_007 #1
    这是和老师的一模一样,但是我发现这样写不能排除主播放列表重复添加相同id的歌曲
    2019-03-26 08:34:50
  • irista23 回复 提问者 士林健津_007 #2
    建议同学把先关代码贴到问答区 这样方便老师帮你找到问题 注意尽量不要截图
    2019-03-26 11:39:48
irista23 2019-03-24 13:54:50

addToPlayList()方法里要排除重复添加的情况,可以使用循环遍历方式检查List<Song>中是否有传入的Song对象,没有再执行添加操作否则给出提示不执行添加。


  • 提问者 士林健津_007 #1
    你好,能说的具体点吗
    2019-03-25 16:26:14
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码