2015-05-03 59 views
0

我在嘗試將用戶輸入保存到變量中,然後再訪問該變量的內容時遇到了一些麻煩。基本上,用戶輸入關於歌曲的詳細信息,然後將其保存在下一個可用歌曲插槽中(總共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方法添加另一個參數或我在做一些完全錯誤的?任何幫助將不勝感激,謝謝!

+0

你已經離開了字段的聲明等等,以及(據說)顯示「插槽」是「完全空的」的代碼。 –

回答

0

最好的辦法是使用構造

song1 = new song("Name of song:","Artist:","File size (MB):","Duration (seconds):") 

同樣的事情在其他對象

然後,你需要添加你的歌曲類中的方法

public boolean isEmpty() { 
     if (this.name == null && this.artist == null && this.fileSize == null && this.duration == null) return true; 
     else return false; 
    }