我在尝试将用户输入保存到变量中,然后访问该变量的内容时遇到了一些麻烦。基本上,用户输入关于歌曲的细节,然后将其保存在下一个可用的歌曲槽中(总共4个槽)。但是当我检查一个插槽是空的还是填充时,它总是空的,因为最后一首歌的细节没有保存。
代码:
SongDatabase类:
if (song1.getFileSize() == 0) {
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());
}
else if (song2.getFileSize() == 0) {
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());
}
else if (song3.getFileSize() == 0) {
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());
}
else if (song4.getFileSize() == 0) {
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());
}
else {
System.out.println("The database is currently full. Please delete a song before adding a new one.");
}
歌曲课程:
public void setName(String inputName) {
name = inputName;
}
public String getName() {
return name;
}
public void setArtist(String inputArtist) {
artist = inputArtist;
}
public String getArtist() {
return artist;
}
public void setFileSize(int inputFileSize) {
fileSize = inputFileSize;
}
public int getFileSize() {
return fileSize;
}
public void setDuration(int inputDuration) {
duration = inputDuration;
}
public int getDuration() {
return duration;
}
我是否需要在get / set方法中添加另一个参数,或者我做错了什么?非常感谢任何帮助,谢谢!