2015-11-08 59 views
0

我試圖在每次循環結束時打印歌曲名稱和歌曲長度。我該怎麼做呢? Report+= songTitles[numSongs] + songLengths[numSongs]打印數組中的值列表

然後,我需要做一個線性搜索從播放列表中刪除歌曲。我是否需要使用相同的報告字符串讓用戶看到所有歌曲?我只需要幫助。謝謝。

import javax.swing.JOptionPane; 

public class asdf_Playlist { 

    public static void main(String[] args) { 

    final int MAX_SONGS = 106; 
    int totalDuration = 0; 
    int numSongs = 0; 
    boolean exitVar = false; 
    int i = 0; 

    String[] songTitles = new String[MAX_SONGS]; 
    int[] songLengths = new int[MAX_SONGS]; 

    while (exitVar == false && numSongs <= songTitles.length) { 

     do { 

     songTitles[numSongs] = JOptionPane.showInputDialog(null,"Enter a song name, or type -1 to exit"); 
     if (songTitles[numSongs].equals("")) { 
      JOptionPane.showMessageDialog(null,"Error: Please enter a valid song name, or type -1 to exit"); 
     } else if (songTitles[numSongs].equals("-1")) { 
      exitVar = true; 
     } 
     } while (songTitles[numSongs].equals("")); 


     do { 
     try { 
      songLengths[numSongs] = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter a song length, e.g. 4.")); 
      if (songLengths[numSongs] > 0) { 
      totalDuration += songLengths[numSongs]; 
      } else { 
      songLengths[numSongs] = -1; 
      JOptionPane.showMessageDialog(null,"Error: please enter a valid song length, e.g. 4."); 
      } 
     } catch (NumberFormatException e) { 
      songLengths[numSongs] = -1; 
      JOptionPane.showMessageDialog(null, "Error: please enter a valid song length, e.g. 4."); 
     } 

     } while (songLengths[numSongs] <= 0); 



     boolean addMore = true; 

     while ((numSongs <= MAX_SONGS) && (addMore == true)) { 
     JOptionPane.showMessageDialog(null, "Song #" + (i+1) + ": " + songTitles[i] + " length: " + songLengths[i] + "\n"); 
     i++; 
     if (songTitles[i] == null) { 
      addMore = false; 
     } 
     } 
     numSongs++; 
    } 
    } 
} 
+2

目前還不清楚你在問什麼。你的意思是打印到控制檯或文件?您是否想要爲循環的每次迭代打印所有歌曲?你問題中的「報告」是指什麼? – sprinter

+0

抱歉 - 我只是想在每次循環重新開始時打印歌曲標題和長度,以便用戶可以看到他們輸入的每首歌曲。這就是我想要做的。 – Hello

+0

我還需要使用線性搜索刪除歌曲,我想我可以使用帶有報告標題的字符串(類似於「歌曲名稱:,歌曲長度:」)來顯示所有歌曲,以便用戶可以鍵入將歌曲從列表中刪除的名稱。 我剛纔發現我可以使用連接並拾取之前輸入信息的字符串,因此用戶可以看到輸入的每首歌曲,而不僅僅是前一首。 – Hello

回答

0

我有幾點建議可以讓你更容易。

首先,你應該創建一個類來捕捉你的歌曲信息,而不是有兩個單獨的數組。從長遠來看,這會讓你的生活變得更容易(而且是更好的OO練習)。然後,您可以創建一個toString方法作爲類的一部分來格式化歌曲信息:

class Song { 
    private final String title; 
    private final int length; 

    public String toString() { 
     return title + ":" + length; 
    } 
} 

你的歌陣列,然後變得更簡單:

private Song[] songs = new Song[MAX_SONGS]; 

打印整個列表可以通過多種方式來實現。 Java的8之前,一般會是這個樣子:

for (Song song: songs) 
    System.out.println(song); 

由於Java 8發佈這可以簡化爲:從數組

Arrays.stream(songs).forEach(System.out::println); 

刪除項目並不像從集合刪除它們一樣簡單。但它仍然不是太難:

Song[] copy = new Song[MAX_SONGS]; 
int copiedSongs = 0; 
for (Song song: songs) 
    if (/* condition for copying */) 
     copy[copiedSongs++] = song; 
songs = copy; 
numSongs = copiedSongs; 

同樣,使用Java 8這已成爲更簡單:

songs = Arrays.stream(songs).filter(/*condition*/).toArray(); 
numSongs = songs.length;