英文:
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("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");
//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 < 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论