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++;
}
}
}
目前还不清楚你在问什么。你的意思是打印到控制台或文件?您是否想要为循环的每次迭代打印所有歌曲?你问题中的“报告”是指什么? – sprinter
抱歉 - 我只是想在每次循环重新开始时打印歌曲标题和长度,以便用户可以看到他们输入的每首歌曲。这就是我想要做的。 – Hello
我还需要使用线性搜索删除歌曲,我想我可以使用带有报告标题的字符串(类似于“歌曲名称:,歌曲长度:”)来显示所有歌曲,以便用户可以键入将歌曲从列表中删除的名称。 我刚才发现我可以使用连接并拾取之前输入信息的字符串,因此用户可以看到输入的每首歌曲,而不仅仅是前一首。 – Hello