有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java简单歌曲列表,使用数组列表和对象

我需要完成这个练习,我应该制作一个简单的歌曲播放器(实际上不播放歌曲),但它应该要求用户输入歌曲名称、时间,将其存储到一个数组中,然后停止,直到用户输入一个空白点,然后交换顺序,和“播放”歌曲(基本上列出歌曲名称,然后在下面写上“娜娜娜娜”,这就是我到目前为止所做的

Song

import java.util.Scanner;

public class Song {

    // ---------- Instance Variables -------------------------------------- //
    private String title;
    private int totalSeconds;
    private String time;

    // ---------- Constructors -------------------------------------------- //
    /**
     * Create a song from its title and running time
     *
     * @param t the title of the song
     * @param l song's run time (<i>mm</i>:<i>ss</i> -- for example: "3:07")
     */
    public Song(String t, String l) {
        java.util.Scanner reader = new Scanner(l);
        reader.useDelimiter(":");
        int mins = reader.nextInt();
        int secs = reader.nextInt();

        setFields(t, mins, secs);
    }

    // ---------- Public Methods ------------------------------------------ //
    /**
     * The length of this Song, in seconds.
     *
     * @return the running time of the song (in seconds)
     */
    public int getLengthInSeconds() {
        return totalSeconds;
    }

    /** 
     * "Play" the song 
     */
    public void play() {
        System.out.print("\nNow Playing: " + title);
        for (int i = 0; i < totalSeconds; i++) {
            if (i % 25 == 0) {
                System.out.println();
            }
            System.out.print("na ");
            try {
                Thread.sleep((int)(100 * Math.random()));
            }
            catch(Exception e) {}
        }
        System.out.println();
    }

    /**
     * Return a string representation of the song, including its title and run
     * time.
     *
     * @return a String for the song: "<i>title</i> (<i>run-time</i>)"
     */
    public String toString() {
        return title + " (" + time + ")";
    }

    // ---------- Private Methods ----------------------------------------- //
    /**
     * Set the all the instance variables in this object.
     * Called by the constructor.
     *
     * @param t title of song
     * @param minutes the running time (minutes part -- the 3 of 3:07)
     * @param seconds the running time's excess seconds (the 7 of 3:07)
     */
    private void setFields(String t, int minutes, int seconds) {
        title = t;
        totalSeconds = 60 * minutes + seconds;
        time = minutes + ":" + (seconds < 10 ? "0": "") + seconds;
    }

}

SongPlayer

import java.lang.reflect.Array;
import java.util.Scanner;

public class SongPlayer {

    public static void main(String[] args) {

        Scanner scnr = new Scanner(System. in );
        int numOfSongs;

        System.out.println("This Player can hold up to 20 songs, and plays them in a random order.");
        System.out.println("Enter the song titles and lengths below. Leave the title blank to end early.");

        System.out.println("How many songs?");
        numOfSongs = scnr.nextInt();
        String songNames[] = new String[numOfSongs];
        int songLength[] = new int[numOfSongs];

        for (int i = 0; i < songNames.length; i++) {
            Scanner scnr2 = new Scanner(System. in );
            System.out.print("Enter song title: ");
            songNames[i] = scnr2.nextLine();
            System.out.print("Enter song Length eg (3:07) : ");
            Scanner reader = new Scanner(System. in );
            reader.useDelimiter(":");
            int mins = reader.nextInt();
            int secs = reader.nextInt();
            System.out.println("The song " + songNames[i] + "(" + mins + ":" + secs + ") has been added.");

        }

    }

}

我应该用Song类来获得时间和所有这些,但我真的不知道该怎么做

输出应该是这样的

Enter song name : First song
Enter song length: 3:07
Song First song (3:07) has been added.

剩下的歌都是这样的

然后交换数组中的顺序

就这样玩吧:

PLaying First Song:
Na na na na na na na na na na na na na na.....

共 (0) 个答案