我需要完成此练习,我应该制作一个简单的歌曲播放器(实际上不播放歌曲),但是它应该向用户询问歌曲名称,时间,并将其存储到数组,然后停止直到用户输入一个空位,然后应该交换顺序,然后“播放”歌曲(基本上列出歌曲名称,然后在其下写“ na na na na na”,这就是我所拥有的到目前为止。
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 :(得分:0)
由于这是您必须要做的练习,因此我不会给出解决方案(代码)。相反,我将给您一些提示。您可以自己尝试。
由于这是一个简单程序,因此可以将Song
类用作简单数据结构。 (例如,请勿在{{1}}中使用Scanner
。)
Song
仅使用一个public class Song {
private String title;
private String time;
}
对象。您可以重复使用它来从用户那里获取多个输入。
您可以使用Scanner
API来随机播放歌曲。
由于以上java.util.Collections.shuffle(java.util.List)
方法采用了shuffle()
作为其参数,因此最好将用户输入的歌曲收集到List
对象(可能是List
)中。
如果您希望用户输入提早结束,那么在使用ArrayList
从用户那里获得歌曲标题之后,您需要检查它是否为空并中断循环:
songNames[i] = scnr2.nextLine();
由于歌曲的持续时间用于打印,因此请考虑然后输入if (songNames[i].isEmpty()) {
break;
}
。这样会使程序更简单。
在获得所有用户输入的歌曲之后,您需要另一个String
循环来打印这些歌曲。