为什么do while循环在这个Java程序中没有按预期运行?
我一直在写一个程序,将采取一个数组的名称的音乐文件,并发挥他们。我成功地做到了这一点,但是,我想改进一些东西,让它变得更好。我试图让音乐以随机顺序播放,但在播放整个列表之前,我不会重复任何歌曲。我几乎可以做到这一点,但我认为我的do-while循环有问题。该程序按预期运行大约8首歌曲,但随后停止播放音乐,JVM继续运行。我正在使用BlueJ,因为我仍然是AP Comp Sci的学生,所以我意识到我可能无法完成这项任务,但任何帮助都将不胜感激。我有一个驱动程序“MusicDriver”,它与另外两个类“MP3”和“Music”有着“has-a”的关系 我的MP3课程:为什么do while循环在这个Java程序中没有按预期运行?,java,do-while,Java,Do While,我一直在写一个程序,将采取一个数组的名称的音乐文件,并发挥他们。我成功地做到了这一点,但是,我想改进一些东西,让它变得更好。我试图让音乐以随机顺序播放,但在播放整个列表之前,我不会重复任何歌曲。我几乎可以做到这一点,但我认为我的do-while循环有问题。该程序按预期运行大约8首歌曲,但随后停止播放音乐,JVM继续运行。我正在使用BlueJ,因为我仍然是AP Comp Sci的学生,所以我意识到我可能无法完成这项任务,但任何帮助都将不胜感激。我有一个驱动程序“MusicDriver”,它与另外两
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3 {
String filename;
Player player;
public void stopMP3() { if (player != null) player.close(); }
// play the MP3 file to the sound card
public void playMP3(String filename) {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
new Thread() {
public void run() {
try { player.play(); }
catch (Exception e) { System.out.println(e); }
}
}.start();
}
}
我的音乐课:
import java.util.*;
public class Music{
private ArrayList<String> music;
public Music(){music = new ArrayList<String>();}
public int size(){return music.size();}
public void addSong(String song){music.add(song);}
public String getSong(){return music.get(music.size());}
public String getSong(int num){return music.get(num);}
public void removeSong(String song){
for(int i = 0; i < music.size(); i++){
if(music.get(i).equals(song)) {music.remove(i); return;}
}
}
public String toString(){
String s = "";
for(int i = 0; i < music.size(); i++){
s += music.get(i);
}
return s;
}
}
import java.util.*;
公共音乐课{
私人阵列音乐;
公共音乐(){Music=newarraylist();}
public int size(){return music.size();}
公共空白添加歌曲(弦乐歌曲){music.add(歌曲);}
公共字符串getSong(){return music.get(music.size());}
公共字符串getSong(int num){return music.get(num);}
公共空间移除歌曲(弦乐曲){
for(int i=0;i
我的音乐驾驶员课程:
import java.util.*;
import java.io.*;
import javazoom.jl.player.Player;
import java.util.Random;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class MusicDriver{
public static void main(String[] args) throws FileNotFoundException{
Random r = new Random();
Scanner s = new Scanner(System.in);
String line = "";
int number;
Music song = new Music();
song.addSong("1-01-overture.mp3");
song.addSong("1-03-fortune-teller-2.mp3");
song.addSong("1-07-prayer.mp3");
song.addSong("1-08-island-atlas.mp3");
song.addSong("1-12-warren-report.mp3");
song.addSong("1-13-avilla-hanya.mp3");
song.addSong("1-20-war-situation.mp3");
song.addSong("2-10-fog-of-phantom.mp3");
song.addSong("2-12-religious-precepts.mp3");
song.addSong("2-14-box-of-sentiment.mp3");
song.addSong("3-02-light-everlasting.mp3");
song.addSong("3-09-viking-spirits.mp3");
song.addSong("3-12-unsealed.mp3");
song.addSong("3-16-notice-of-death-reprise-.mp3");
//14 songs
ArrayList<Integer> songNums = new ArrayList<Integer>();
MP3 mp3 = new MP3();
do{
if(songNums.size() == song.size()) songNums.clear();
number = r.nextInt(song.size());
boolean done = false;
int counter = 0;
while(!done){
for(int i = 0; i < songNums.size(); i++){
if(number == songNums.get(i).intValue()) {number = r.nextInt(song.size()); counter++;}
}
if(counter == 0) done = true;
else done = false;
}
songNums.add(number);
mp3.playMP3(song.getSong(number));
System.out.println("Now Playing " + song.getSong(number));
System.out.println("Enter \"Stop\" to stop playing the song");
System.out.println("Enter \"n\" to play the next song");
line = s.nextLine();
mp3.stopMP3();
}while(line.equals("n"));
mp3.stopMP3();
}
}
import java.util.*;
导入java.io.*;
导入javazoom.jl.player.player;
导入java.util.Random;
导入java.util.Scanner;
导入java.io.FileNotFoundException;
公共类音乐驱动器{
公共静态void main(字符串[]args)引发FileNotFoundException{
随机r=新随机();
扫描仪s=新的扫描仪(System.in);
字符串行=”;
整数;
音乐歌曲=新音乐();
song.addSong(“1-01-overture.mp3”);
song.addSong(“1-03-fortune-teller-2.mp3”);
song.addSong(“1-07-prayer.mp3”);
song.addSong(“1-08-island-atlas.mp3”);
addSong(“1-12-warren-report.mp3”);
song.addSong(“1-13-avilla-hanya.mp3”);
song.addSong(“1-20-war-contence.mp3”);
song.addSong(“2-10-fog-of-phantom.mp3”);
song.addSong(“2-12-宗教戒律.mp3”);
song.addSong(“2-14-box-of-touction.mp3”);
song.addSong(“3-02-light-eventher.mp3”);
song.addSong(“3-09-viking-spirits.mp3”);
song.addSong(“3-12-unchaled.mp3”);
song.addSong(“3-16-notice-of-death-reprise-.mp3”);
//14首歌
ArrayList songNums=新的ArrayList();
MP3=新的MP3();
做{
如果(songNums.size()==song.size())songNums.clear();
number=r.nextInt(song.size());
布尔完成=假;
int计数器=0;
而(!完成){
对于(int i=0;i
我做了很多关于为什么我的程序停止播放我的歌曲的研究,但是我没有找到任何东西。我发现,如果在输出之前请求输入,BlueJ程序不会打开终端窗口(当您执行“System.out.print()”),但我认为这个程序没有考虑到这一点。我还确保在播放下一首歌时输入了字符串“n”,对于前几首歌,它可以工作,但在第八首歌之后,它就停止了。我完全搞糊涂了。我认为唯一的问题在于你用来洗牌列表的逻辑
number = r.nextInt(song.size());
boolean done = false;
int counter = 0;
while(!done){
for(int i = 0; i < songNums.size(); i++){
if(number == songNums.get(i).intValue()) {number = r.nextInt(song.size()); counter++;}
}
if(counter == 0) done = true;
else done = false;
}
或者,您可以在注释中使用的建议来洗牌列表(Collections.shuffle())。现有算法的实际问题是,当您发现已播放的歌曲时,您没有重置
计数器。所以,一旦你重复了一次,你就会陷入一个无限循环中-done
永远不会是真的
(实际上,它不会是无限的-一旦计数器
达到整数。MAX_VALUE
它将环绕到整数。MIN_VALUE
并最终再次达到0
,因此如果您将其保留足够长的时间,它最终将播放另一首歌曲)
关于代码的改进,这里已经有一些有用的建议,我在这里不再重复,但要解决您的问题,最基本的改变是将计数器的初始化移动到0
循环中:
boolean done = false;
while(!done){
int counter = 0; // reset counter every time
for(int i = 0; i < songNums.size(); i++){
if(number == songNums.get(i).intValue()) {number = r.nextInt(song.size()); counter++;}
}
if(counter == 0) done = true;
else done = false;
}
boolean done=false;
而(!完成){
int counter=0;//每次重置计数器
对于(int i=0;i萨沙在评论中说:使用Collections.shuffle()。实际上,这看起来像是一个很小的例子:
在音乐课上,有一种获取所有歌曲的方法:
public List<String> getSongs() {return music;}
public List getSongs(){return music;}
MusicDriver中的循环将沿着以下线路:
List<String> songs = song.getSongs();
do{
Collections.shuffle(songs);
for (String songToPly: songs) {
mp3.playMP3(song.getSong(number));
System.out.println("Now Playing " + song.getSong(number));
System.out.println("Enter \"Stop\" to stop playing the song");
System.out.println("Enter \"n\" to play the next song");
mp3.stopMP3();
line = s.nextLine();
if (!line.equals("n")) break;
}
}while(line.equals("n"));
List songs=song.getSongs();
做{
收藏。洗牌(歌曲);
用于(弦乐歌曲主题:歌曲){
mp3.playm3(song.getSong(number));
System.out.println(“正在播放”+song.getSong(数字));
System.out.println(“输入\“停止\”停止播放歌曲”);
System.out.println(“输入\“n\”播放下一首歌”);
mp3.stopp3();
List<String> songs = song.getSongs();
do{
Collections.shuffle(songs);
for (String songToPly: songs) {
mp3.playMP3(song.getSong(number));
System.out.println("Now Playing " + song.getSong(number));
System.out.println("Enter \"Stop\" to stop playing the song");
System.out.println("Enter \"n\" to play the next song");
mp3.stopMP3();
line = s.nextLine();
if (!line.equals("n")) break;
}
}while(line.equals("n"));
public class MainClass() {
public static void main(String[] args) {
PlayerWrapper player = new PlayerWrapper();
}
}
public class PlayerWrapper() {
private List<MP3> playlist;
private Scanner userInputReader;
private String currentUserInput;
public PlayerWrapper() {
userInputReader = new Scanner(System.in());
System.out.println("Filepath to playlist?");
String playlistFileName = userInputReader.nextLine();
playlist = PlayListExtractor.extractPlaylist(playlistFileName);
start();
}
public void start() {
playlistCopy = new ArrayList<MP3>(playlist);
shufflePlayList(playlistCopy);
Iterator<MP3> songIterator = playlistCopy.iterator();
while (songIterator.hasNext()) {
MP3 song = songIterator.next();
songIterator.remove();
player = new Player(song.toStream());
player.play();
displayCurrentSongAndCommands(song);
currentUserInput = userInputReader.nextLine();
if ("Stop".equals(currentUserInput )) {
player.close();
break;
} else if ("n".equals(currentUserInput )) {
player.close();
continue;
}
}
if("Stop".equals(currentUserInput)) {
System.out.println("Playlist stopped. Press q to quit or c to continue");
currentUserInput = userInputReader.nextLine();
if ("q".equals(currentUserInput)) {
System.exit(0);
} else if ("c".equals(currentUserInput)) {
start();
}
}
start();
}
private void shufflePlayList(final List<MP3> playlistToBeShuffled) {
long seed = System.nanoTime();
Collections.shuffle(playlistToBeShuffled, new Random(seed));
}
private void displayCurrentSongAndCommands(final MP3 currentSong) {
System.out.println("Now Playing " + currentSong.toString());
System.out.println("Enter \"Stop\" to stop playing the song");
System.out.println("Enter \"n\" to play the next song");
}
}
public static class PlayListExtractor() {
private PlayListExtractor();
public static List<MP3> extractPlayList(final String playListFileName) {
List<MP3> result = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
result.add(new MP3(line));
}
return result;
} catch (IOException e) {
System.out.println("Problem parsing playlist");
}
}
}
public class MP3 {
private String filename;
public MP3(final String filename) {
this.filename = filename;
}
public BufferedInputStream toStream() {
try {
FileInputStream fis = new FileInputStream(filename);
return new BufferedInputStream(fis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
}
public String toString() {
return filename;
}
}