Java 更新的输出正在与旧输出连接
有人能找出为什么在删除找到的值后,输出包含删除之前的所有信息吗Java 更新的输出正在与旧输出连接,java,Java,有人能找出为什么在删除找到的值后,输出包含删除之前的所有信息吗 // Prints current items in both arrays String titles = ""; String lengths = ""; for (int i = 0; i < numOfSongs; i++) { titles += songTitles[i] + " "; lengths += songLengths[i] + " "; } JOptionP
// Prints current items in both arrays
String titles = "";
String lengths = "";
for (int i = 0; i < numOfSongs; i++) {
titles += songTitles[i] + " ";
lengths += songLengths[i] + " ";
}
JOptionPane.showMessageDialog(null, "**Current Playlist**" + "\nSong titles: " + titles + "\nSong lengths: " + lengths);
// Determines if the user wants to remove a song from the current list
boolean found = false;
// If search matches a song in array, set title to null and length to 0
for (int i = 0; i < songTitles.length; i++) {
if (search.equalsIgnoreCase(songTitles[i])) {
found = true;
songTitles[i] = null;
songLengths[i] = 0;
}
}
// Update arrays, song count, and duration across all songs
if (found) {
titles += songTitles[numOfSongs] + " ";
lengths += songLengths[numOfSongs] + " ";
totalDuration -= songLengths[numOfSongs];
numOfSongs--;
}
// Print updated playlist
JOptionPane.showMessageDialog(null, "**Current Playlist**" + "\nSong titles: " + titles + "\nSong lengths: " + lengths);
//打印两个数组中的当前项
字符串标题=”;
字符串长度=”;
for(int i=0;i
标题
和总持续时间
字符串用歌曲标题
和歌曲长度
中的所有元素初始化
如果在歌曲标题
中找到搜索
,则将其从歌曲标题
中删除,但不更新歌曲标题
。相反,您可以从songTitles附加更多歌曲标题
您可能希望清除songTitles
和SongLength
并跳过songTitles
中的空值重新创建它们。例如
titles = "";
lengths = "";
for (int i = 0; i < numOfSongs; i++) {
if (songTitles[i] != null) {
titles += songTitles[i] + " ";
lengths += songLengths[i] + " ";
}
}
titles
和totalDuration
字符串用songtheals
和songslength
中的所有元素初始化
如果在歌曲标题
中找到搜索
,则将其从歌曲标题
中删除,但不更新歌曲标题
。相反,您可以从songTitles附加更多歌曲标题
您可能希望清除songTitles
和SongLength
并跳过songTitles
中的空值重新创建它们。例如
titles = "";
lengths = "";
for (int i = 0; i < numOfSongs; i++) {
if (songTitles[i] != null) {
titles += songTitles[i] + " ";
lengths += songLengths[i] + " ";
}
}
下面的语句导致与旧值连接
titles += songTitles[numOfSongs] + " ";
lengths += songLengths[numOfSongs] + " ";
在添加新值之前,应首先通过将字符串设置为空来清除现有值
titles = "";
lengths = "";
下面的语句导致与旧值连接
titles += songTitles[numOfSongs] + " ";
lengths += songLengths[numOfSongs] + " ";
在添加新值之前,应首先通过将字符串设置为空来清除现有值
titles = "";
lengths = "";
我不完全确定我是否理解您的问题-您的问题似乎是由于在代码开头使用“+=”连接标题和长度造成的。然后执行“查找检查”,继续向同一变量添加数据,而不通过执行titles=“”和length=“”之类的操作重置变量。我不完全确定我是否理解您的问题-您的问题似乎是由于在代码开头使用“+=”连接标题和长度造成的。然后执行“查找检查”,继续向同一变量添加数据,而无需通过执行诸如titles=“”和length=“”之类的操作重置变量