java 中 ListIterator 中的 next() 和 previous() 方法的问题


import java.util.Scanner;
public class Main_songs {
public static Scanner scanner=new Scanner(System.in);
public static PlayList play=new PlayList();
public static void main(String[] args) {
boolean quit=false;
while(!quit) {
System.out.println("Enter your choice");
System.out.println("0.quitn"+
"1.Add song in playlistn"+
"2.remove song from playlistn"+
"3.forward songn"+
"4.backward songn"+
"5.replay current songn"+
"6.list all songsn"+
"7.create new albumn"+
"8.Add songs to album");
int choice=scanner.nextInt();
scanner.nextLine();
switch(choice) {
case 0:
quit=true;
break;
case 1:
System.out.println("Enter the album name");
String album_name=scanner.nextLine();
Album album=play.findAlbum(album_name);
if(album!=null) {
System.out.println("Enter the song tilte");
String title=scanner.nextLine();
Song song=album.findSong(title);
if(song!=null) {
play.addSongs(album_name, song);
System.out.println("Song added in the playlist");}
else {
System.out.println("Song not found in the album");}}
else
System.out.println("album not found");
break;
case 2:
System.out.println("Enter the song title");
String title=scanner.nextLine();
play.removeSong(title);
break;
case 3:
play.skipForward();
break;
case 4:
play.skipBackward();
break;
case 5:
play.Replay();
break;
case 6:
play.listSongs();
break;
case 7:
System.out.println("Enter the album name");
String album_title=scanner.nextLine();
play.createNewAlbum(album_title);
break;
case 8:
System.out.println("Enter the album name");
String albumName=scanner.nextLine();
System.out.println("Enter the song name");
String song_name=scanner.nextLine();
System.out.println("Enter the song duration");
double duration=scanner.nextDouble();
play.addSongtoAlbum(albumName, song_name, duration);
break;
}
}
}
package linkedList;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Iterator;
public class PlayList {
private LinkedList<Song> playlist=new LinkedList<Song>();
private ArrayList<Album> album_list;
private ListIterator<Song> list=playlist.listIterator();
public PlayList() {
this.album_list=new ArrayList<Album>();
}
public LinkedList<Song> getPlaylist(){
return this.playlist;
}
public ArrayList<Album> getAlbum_list(){
return this.album_list;
}
public void addSongs(String album_name,Song song) {
playlist.add(song);
}
public void listSongs() {
for(int i=0;i<playlist.size();i++) {
Song song=playlist.get(i);
System.out.println("Songs are "+song.getTitle() +" and duration is "+song.getDuration());}
}
public void createNewAlbum(String album_name) {
Album album=findAlbum(album_name);
if(album==null) {
album_list.add(new Album(album_name));
System.out.println("New album created succesfully");
}else
System.out.println("This album is already exist");
}
public Album findAlbum(String title) {
for(int i=0;i<album_list.size();i++) {
Album album=album_list.get(i);
if(album.getAlbum_name().equals(title)) {
return album;
}
}return null;}
public void skipForward() {
list = playlist.listIterator();
if (list.hasNext()) {
list.next();
if (list.hasNext()) {
System.out.println("Playing next song " + list.next().getTitle());
} else {
System.out.println("Currently playing last song");
}
}
}
public void skipBackward() {
list = playlist.listIterator();
if (list.hasPrevious()) {
list.previous();
if (list.hasPrevious()) {
System.out.println("Playing previous song " + list.previous().getTitle());
} else {
System.out.println("Currently playing last song");
}
}
}
public void Replay() {
boolean goingForward=true;
if(goingForward) {
if(list.hasPrevious()) {
System.out.println("Now replaying "+list.previous().getTitle());
goingForward=false;
}else
System.out.println("Now playlist at the start");}
else
if(list.hasNext()) {
System.out.println("Now replaying "+list.next().getTitle());
goingForward=true;
}else
System.out.println("Playlist come to end");
}
public void removeSong(String title) {
if(playlist.size()>0) {
list.remove();
System.out.println("Deleted current song");
if(list.hasNext()) {
System.out.println("Now you are playing "+list.next().getTitle());
}
if(list.hasPrevious()) {
System.out.println("Now you are playing "+list.previous().getTitle());
}
}
}
public void addSongtoAlbum(String album_name,String title,double duration) {
Album album=findAlbum(album_name);
if(album!=null) {
if(album.getAlbum_name().equals(album_name)) {
album.addSongs(title, duration);
}else
System.out.println("Album not exist");
}else
System.out.println("This album not exist");
}
}

package linkedList;
import java.util.ArrayList;
public class Album {
private String album_name;
private ArrayList<Song> song_list=new ArrayList<Song>();
public Album(String album_name) {
this.album_name=album_name;
}
public String getAlbum_name() {
return album_name;
}
public ArrayList<Song> getSongs(){
return this.song_list;
}
public boolean addSongs(String title,double duration) {
Song song=findSong(title);
if(song==null) {
song_list.add(new Song(title,duration));
System.out.println("song added successfully");
return true;
}else {
System.out.println("This song already exist in the list");
return false;
}
}
public Song findSong(String title) {
for(int i=0;i<song_list.size();i++) {
Song song=song_list.get(i);
if(song.getTitle().equals(title)) {
return song;
}
}return null;
}
}
package linkedList;
public class Song {
private String title;
private double duration;
public Song(String title,double duration) {
this.title=title;
this.duration=duration;
}
public String getTitle() {
return title;
}
public double getDuration() {
return duration;
}
}

你好 我发布了部分代码。该程序基于带有列表迭代器概念的链表。创建一个相册。从该专辑中选择歌曲并放入播放列表。在这里,我有一个向前和向后移动功能的问题。转发功能仅转发一次。然后它重复同一首歌。后向函数不起作用。它只是打印以前的无歌曲。谁能帮我,给我一些想法。谢谢。

在我的代码中,它正在创建专辑并添加歌曲,然后将专辑中的歌曲添加到播放列表,最后打印播放列表中的所有歌曲。之后在 forward(( 方法中,它只是转发一首歌。然后它重复同一首歌。在 back(( 方法中,它只检查 hasPrevious((。在它无法进一步进行之后。重播方法也一样,就是说列表的开头。Remove(( 方法说 Exception illegalStateException。

任何人都可以检查我的代码并给我反馈。

在每次next调用之前hasNext.previous也是如此.

Song sing = list.next();
System.out.println("Playing next song "+sing.getTitle());

应该是

Song sing = list.next();
System.out.println("Playing next song "+sing.getTitle());

或(因为那个"跳过"(

list.next();
if (list.hasNext()) {
System.out.println("Playing next song "+list.next().getTitle());
}

相关内容

  • 没有找到相关文章

最新更新