变量即使在为它们分配值之后也为无效(Java)



我正在尝试编写一种将歌曲信息(用户输入)添加到数据库中的方法。用户添加歌曲详细信息(名称,艺术家,文件大小,持续时间)后,应将其保存在第一个空的歌曲插槽中(总计4个歌曲插槽),然后返回菜单界面。

但是,当我尝试添加第二首歌曲时,第一个插槽总是空的,好像用户输入未保存的详细信息一样。我已经尝试踏上调试器,当我输入名称,艺术家等时,它们都保存在Song1对象中。但是,当我回去输入第二首歌时,Song1对象具有值:name = null,Artist = null,Filesize = 0,持续时间= 0。

我已经呆了几个小时了,真的很困惑,任何帮助都会很棒!

songdatabase类:

public class SongDatabase {
Scanner console = new Scanner(System.in);
public void addNewSong() {
    Song song1 = new Song();
    Song song2 = new Song();
    Song song3 = new Song();
    Song song4 = new Song();

if (song1.isEmpty()) {        
    System.out.println("Name of song:");
    song1.setName(console.next());
    System.out.println("Artist:");
    song1.setArtist(console.next());
    System.out.println("File size (MB):");
    song1.setFileSize(console.nextInt());
    System.out.println("Duration (seconds):");
    song1.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}
else if (song2.isEmpty()) {        
    System.out.println("Name of song:");
    song2.setName(console.next());
    System.out.println("Artist:");
    song2.setArtist(console.next());
    System.out.println("File size (MB):");
    song2.setFileSize(console.nextInt());
    System.out.println("Duration (seconds):");
    song2.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}
else if (song3.isEmpty()) {        
    System.out.println("Name of song:");
    song3.setName(console.next());
    System.out.println("Artist:");
    song3.setArtist(console.next());
    System.out.println("File size (MB):");
    song3.setFileSize(console.nextInt());
    System.out.println("Duration (seconds):");
    song3.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}
else if (song4.isEmpty()) {        
    System.out.println("Name of song:");
    song4.setName(console.next());
    System.out.println("Artist:");
    song4.setArtist(console.next());
    System.out.println("File size (MB):");
    song4.setFileSize(console.nextInt());
    System.out.println("Duration (seconds):");
    song4.setDuration(console.nextInt());
    System.out.println("Song successfully added.");
    System.out.println("");
}
else {
    System.out.println("The database is currently full. Please delete a song before adding a new one.");
}

歌曲类:

public class Song {
private String name, artist;
private int fileSize, duration;
public Song(String name, String artist, int fileSize, int duration) {
    name = "";
    artist = "";
    fileSize = 0;
    duration = 0;
}
public Song(){}
public boolean isEmpty() {
    if (this.name == null && this.artist == null && this.fileSize == 0 && this.duration == 0 ) return true;
    else return false;
}
public void setName(String inputName) {
    inputName = name;
}
public String getName() {
    return name;
}
public void setArtist(String inputArtist) {
    artist = inputArtist;
}
public String getArtist() {
    return artist;
}
public void setFileSize(int inputFileSize) {
    if (inputFileSize>0){
        fileSize = inputFileSize;
    }
}
public int getFileSize() {
    return fileSize;
}
public void setDuration(int inputDuration) {
    if (inputDuration>0) {
    duration = inputDuration;
    }
}
public int getDuration() {
    return duration;
}
}

addNewSong()方法的输入时,您可以创建四个Song对象,然后将它们存储在本地变量中。退出方法时,本地变量会丢失,因此每次输入addNewSong()时,您都会有四首全新的歌曲。您可能需要将歌曲存储在课堂字段中,并在hard之前初始化它们(类似于您的console字段)。只需将这四行从addNewSong()方法移出:

Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();

还建议您考虑使用阵列或List的歌曲。当您添加新功能或增加歌曲数时,您的代码将很快变成一团糟。

您每次调用addNewSong()方法时都在初始化Song1..4。您需要将初始化外部化,可能是构造函数。

顺便说一句,我考虑使用ArrayList<Song>进行更灵活的实现

首先,您需要了解局部变量和范围。其次,如果固定插槽可以更好地使用阵列。

Song[] songs = new Songs[4];
for(int i=0;i<songs.length;i++){
   // Get input for song from user and use constructor
   songs[i] = new Song(name,artist,size,duration);
}

当您有构造函数时,为什么需要单独设置属性?您应该已经使用了构造函数本身。如果插槽不是固定的arrayList。

List<Song> songsList = new ArrayList<Song>();
// Get songs from user and add them to list.

相关内容

最新更新