如何调出歌曲的Id进行比较
1 | <br> |
我在测试时,发现可以重复向主播放列表添加id相同的歌曲,我想在输入id后,能够进行判断主播放列表中有没有相同的id歌曲。有的话提示,没有继续添加。但我不会编。
1 | <br> |
2
收起
正在回答
4回答
老师用你贴的代码测试添加重复数据是不能添加的,你如果能正常添加查看一下你的Song类的equals()方法是否正确。可以把equals()单独贴出来
士林健津_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); } } |
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧