【发布时间】:2015-11-08 21:19:13
【问题描述】:
http://puu.sh/l3owc/e16f0fe76f.png(讲师提示)
基本上,我试图让用户通过为歌曲标题输入输入 -1 来退出循环。出于某种原因,它对我不起作用,它只是将值 -1 作为歌曲名称。我遇到的另一个问题是,在输入每首歌曲后,我正在尝试按提示打印“所有剩余歌曲”。对我来说,当我希望它显示所有(我的提示是什么意思)输入的歌曲时,它只会删除前一个并显示最近的歌曲标题和长度。
然后,用户可以删除歌曲,之后应该会显示一个报告。我应该使用某种会不断添加的报告字符串吗?不知道该怎么做...我快要弄清楚了,只需要一些帮助。非常感谢这个网站的好心人
import javax.swing.JOptionPane;
public class IT106_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++;
}
}
}
【问题讨论】: