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

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

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

问题

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

private void prepareSongs()
{
    Song theWayYouLookTonight = new Song("S1001", "The Way You Look Tonight","Michael Buble","a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=null",4.39,"michael_buble_collection");
    Song billieJean = new Song("S1002","Billie Jean","Michael Jackson","4eb779428d40d579f14d12a9daf98fc66c7d0be4?cid=null",5.45,"billie_jean");
    Song outinSpace = new Song("S1003","Out in Space","Whiz Khalifa","3d691281fae7d4ba8c5907887d2a31ce064891b3?cid=null",2.83,"wiz_khalifa");

    //这是我的歌曲数组
    songs[0] = theWayYouLookTonight;
    songs[1] = billieJean; 
    songs[2] = outinSpace;
}

//洗牌方法
public Song getShuffleSong(String currentSongId)
{
    Song song = null;

    for(int i = 0; i < songs.length; i++)
    {
        if(songs[i].getId().equals(currentSongId))
        {
            //这部分代码让我感到困扰
            song = songs[(int)(Math.random() * 3)]; 
            break;
        }
    }
    return song;
}
英文:

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.

 private void prepareSongs()
{
    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;);
    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;);
    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;);

    //This is my songs array
    songs[0] = theWayYouLookTonight;
    songs[1] = billieJean; 
    songs[2] = outinSpace;
}

//Shuffle method
 public Song getShuffleSong(String currentSongId)
{
    Song song = null;

    for(int i = 0; i &lt; songs.length; i++)
    {
        if(songs[i].getId().equals(currentSongId))
        {
            //This part of the code is bugging me 
            song = songs[(int)(Math.random() * 3)]; 
            break;
        }
    }
    return song;
}

答案1

得分: 1

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

public Song getShuffleSong(String currentSongId) {
	int rand = (int) (Math.random() * songs.length);
	while (songs[rand].getId().equals(currentSongId)) {
		rand = (int) (Math.random() * songs.length);
	}
	return songs[rand];
}

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

英文:

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

public Song getShuffleSong(String currentSongId) {
	int rand = (int) (Math.random() * songs.length);
	while (songs[rand].getId().equals(currentSongId)) {
		rand = (int) (Math.random() * songs.length);
	}
	return songs[rand];
}

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:

确定