如何为Java

时间:2016-03-11 02:39:30

标签: java arraylist

我的老师做了一项任务,我很难解决。我对Java不是很熟练,但任何帮助都会受到赞赏。

我们应该创建一个基于DOS的“音乐”程序,让人们可以播放,随机播放和创建自己的歌曲播放列表。

我已经弄清楚如何做前2个,所以我只需要帮助让用户创建自己的播放列表。

public class MusicPlayer 
{
   //This is the directory that we will store songs in, you could change 
   //   this here if you wanted to use a different set of songs.
   private static final String songDirectory = "songs/";
   private static Scanner in = new Scanner(System.in);
   //The compiler is going to give you a bunch of warnings about this, 
   //   let's ignore those :)
   private static AudioStream stream = null;


   public static void main(String[] args)
   {
      ArrayList<String> songs = loadSongs(songDirectory);

      songs = customizePlaylist(songs);

      System.out.println("---------------MUSIC STARTED---------------");
      int currentTrack = 0;
      do {
         String currentSong = songDirectory + songs.get(currentTrack++);
         System.out.println("Currently playing... " + currentSong);
         playSong(currentSong);
         System.out.println("Press enter to hear the next song.");
         in.nextLine();
      } while(currentTrack < songs.size());

      System.out.println("--------------END OF PLAYLIST--------------");
   }

   //TODO: Customize your playlist by allowing songs to be reordered, 
   //      removed, or shuffled.
   public static ArrayList<String> customizePlaylist(ArrayList<String> songs)
   {
      return songs;
   }

   //TODO: List all the songs in the songlist with "track numbers" in front   
   public static void listSongs(ArrayList<String> songlist)
   {
     int x = 0;
     for (final File fileEntry : folder.listFiles()) {
         //make sure the file isn't a directory
         if (!fileEntry.isDirectory()) 
            //check if the file ends with .wav
            if(fileEntry.getName().endsWith("-clip.wav")) {
               System.out.println("\t" + (x+1) + ". " + fileEntry.getName());
            }
      }
   }

   //TODO: Shuffle the songlist randomly by choosing a random index and
   //      moving the song at that index to the end of the list a number of times
   public static ArrayList<String> shuffle(ArrayList<String> songlist)
   {
      Collections.shuffle(arrayList);
      return shuffle;
   }

   //This method loads all the song names in the specified directly into an ArrayList
   public static ArrayList<String> loadSongs(String directory)
   {
      System.out.println("==============LOADING SONGS================");
      System.out.println("   DIRECTORY " + directory);
      //create a new ArrayList to store all the song titles
      ArrayList<String> fileList = new ArrayList<String>();
      //open the folder
      final File folder = new File(directory);
      //go through all the files in the folder
      for (final File fileEntry : folder.listFiles()) {
         //make sure the file isn't a directory
         if (!fileEntry.isDirectory()) 
            //check if the file ends with .wav
            if(fileEntry.getName().endsWith("-clip.wav")) {
               System.out.println("\tLoading " + fileEntry.getName());
               //add the file name to our song list
               fileList.add(fileEntry.getName());
            }
      }
      System.out.println("===========================================\n\n");
      return fileList;
   }

   //This method plays a given song. It also stops any song that is already playing.
   public static void playSong(String songpath)
   {
      try {
         //if there's a stream already playing, stop it.
         AudioPlayer.player.stop(stream);
         // Open an input stream  to the audio file.
         InputStream songstream = new FileInputStream(songpath);
         // Create an AudioStream object from the input stream.
         stream = new AudioStream(songstream);         
         // Use "player" from class AudioPlayer to play clip.
         AudioPlayer.player.start(stream);
      } catch(Exception e) {
         System.out.println("PROBLEM WITH THE AUDIOSTREAM.");
         e.printStackTrace();
      }
   }

我没有使用过ArrayList(对它来说很新)的功能太多而且我不确定你是否可以直接做到

public static ArrayList<String> customizePlaylist(ArrayList<String> songs)
   {
      ArrayList<String> custPlayList = songs;
      bool userDone = false;
      while(userDone == false)
      {
        System.out.println("What would like to do with your playlist? (1) add a song, (2) remove a song from your playlist " );
        2n = in.nextInt(); // Scans the next token of the input as an int.
        if(n == 1)
        {
            System.out.println("What song would you like to add? " );
            n = in.nextInt();
            custPlayList.add(songs.get(n-1));
        }
        if(n == 2)
        {
            System.out.println("What song would like to remove? " );
            n = in.nextInt();
            custPlaylist.remove(n-1);
        }
        else 
        {
            System.out.println("You did not enter a correct number, continuing" );
        }
        System.out.println("Your current playlist looks like ");
        System.out.println(custPlayList);
        System.out.println("Are you done? 1. Yes 2. No" );
        n = in.nextInt();
        if(n == 1)
        {
            userDone = true;
        }
        else
        {
            userDone = false;
        }
      }
      return custPlayList;
   }

该程序运行,但它也没有问任何东西。我一直在研究这个问题已经有几个小时了,而我只是因为试图弄清楚这些东西而死脑筋。

由于

0 个答案:

没有答案