当前歌曲在洗牌后出现了重复。我需要指导如何防止重复发生。

huangapple go评论123阅读模式
英文:

The current song duplicates after shuffling. I need guidance in preventing duplicates from happening

问题

我正在尝试开发一个简单的音乐应用程序。我在处理洗牌方法时遇到了困难,因为当前播放的歌曲在我按下洗牌按钮时会被复制。有没有办法可以防止它重复?我不打算使用数组列表和Collections.shuffle方法等更简单的方法。

  1. private void prepareSongs()
  2. {
  3. Song theWayYouLookTonight = new Song("S1001", "The Way You Look Tonight","Michael Buble","a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=null",4.39,"michael_buble_collection");
  4. Song billieJean = new Song("S1002","Billie Jean","Michael Jackson","4eb779428d40d579f14d12a9daf98fc66c7d0be4?cid=null",5.45,"billie_jean");
  5. Song outinSpace = new Song("S1003","Out in Space","Whiz Khalifa","3d691281fae7d4ba8c5907887d2a31ce064891b3?cid=null",2.83,"wiz_khalifa");
  6. //这是我的歌曲数组
  7. songs[0] = theWayYouLookTonight;
  8. songs[1] = billieJean;
  9. songs[2] = outinSpace;
  10. }
  11. //洗牌方法
  12. public Song getShuffleSong(String currentSongId)
  13. {
  14. Song song = null;
  15. for(int i = 0; i < songs.length; i++)
  16. {
  17. if(songs[i].getId().equals(currentSongId))
  18. {
  19. //这部分代码让我感到困扰
  20. song = songs[(int)(Math.random() * 3)];
  21. break;
  22. }
  23. }
  24. return song;
  25. }
英文:

I'm trying to develop a simple music application. I am struggling with the shuffle method as the current song that is playing is getting duplicated as I press the shuffle button. Is there any way I can prevent it from duplicating? I do not plan on doing the easier ways of using array lists and the Collections.shuffle method.

  1. private void prepareSongs()
  2. {
  3. Song theWayYouLookTonight = new Song(&quot;S1001&quot;, &quot;The Way You Look Tonight&quot;,&quot;Michael Buble&quot;,&quot;a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=null&quot;,4.39,&quot;michael_buble_collection&quot;);
  4. Song billieJean = new Song(&quot;S1002&quot;,&quot;Billie Jean&quot;,&quot;Michael Jackson&quot;,&quot;4eb779428d40d579f14d12a9daf98fc66c7d0be4?cid=null&quot;,5.45,&quot;billie_jean&quot;);
  5. Song outinSpace = new Song(&quot;S1003&quot;,&quot;Out in Space&quot;,&quot;Whiz Khalifa&quot;,&quot;3d691281fae7d4ba8c5907887d2a31ce064891b3?cid=null&quot;,2.83,&quot;wiz_khalifa&quot;);
  6. //This is my songs array
  7. songs[0] = theWayYouLookTonight;
  8. songs[1] = billieJean;
  9. songs[2] = outinSpace;
  10. }
  11. //Shuffle method
  12. public Song getShuffleSong(String currentSongId)
  13. {
  14. Song song = null;
  15. for(int i = 0; i &lt; songs.length; i++)
  16. {
  17. if(songs[i].getId().equals(currentSongId))
  18. {
  19. //This part of the code is bugging me
  20. song = songs[(int)(Math.random() * 3)];
  21. break;
  22. }
  23. }
  24. return song;
  25. }

答案1

得分: 1

一个简单的解决方案是持续生成随机索引,直到索引处的歌曲不是当前的歌曲。

  1. public Song getShuffleSong(String currentSongId) {
  2. int rand = (int) (Math.random() * songs.length);
  3. while (songs[rand].getId().equals(currentSongId)) {
  4. rand = (int) (Math.random() * songs.length);
  5. }
  6. return songs[rand];
  7. }

或者,你可以将所有仍可用的索引存储在定期更新的List中,这只需要从该列表中获取一个随机索引。

英文:

A simple solution would be to continuously generate random indexes until the song at the index is not the current song.

  1. public Song getShuffleSong(String currentSongId) {
  2. int rand = (int) (Math.random() * songs.length);
  3. while (songs[rand].getId().equals(currentSongId)) {
  4. rand = (int) (Math.random() * songs.length);
  5. }
  6. return songs[rand];
  7. }

Alternatively, you could store all of the indexes still available in a List that is updated regularly, which would only require obtaining one random index from that list.

huangapple
  • 本文由 发表于 2020年8月2日 05:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63210345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定