java - 简单的歌曲列表,使用数组列表和对象
问题描述
我有这个需要完成的练习,我应该制作一个简单的歌曲播放器(实际上并不播放歌曲),但它应该向用户询问歌曲名称、时间,将其存储到数组中,然后停止,直到用户进入一个空的位置,然后它应该交换顺序,并“播放”歌曲(基本上列出歌曲名称并在其下写“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.....
解决方案
由于这是您必须自己进行的练习,因此我不会给出解决方案(代码)。相反,我会给你一些提示。你可以自己试试。
由于这是一个简单的程序,所以使用Song
类作为简单的数据结构。(例如,不要Scanner
在里面使用Song
。)像这样的东西:
public class Song {
private String title;
private String time;
}
只使用一个Scanner
对象。您可以重用它来获取用户的多个输入。
您可以使用java.util.Collections.shuffle(java.util.List)
API 随机播放歌曲。
由于上述shuffle()
方法以 aList
作为其参数,因此最好将用户输入的歌曲收集到一个List
对象(可能是一个ArrayList
)。
如果您希望用户输入提前结束,那么在从用户 using 获取歌曲名称后songNames[i] = scnr2.nextLine();
,您需要检查它是否为空并打破循环,如:
if (songNames[i].isEmpty()) {
break;
}
由于歌曲的持续时间用于打印,因此考虑将 then 输入为String
s。这将使程序更简单。
在获得所有用户输入的歌曲后,您需要另一个for
循环来打印这些歌曲。
推荐阅读
- python - Lambda 表达式和字符串
- database - 用于大型媒体嵌套元数据查询的最快 AWS DB 服务
- c - if/else if 语句中的条件格式是否必须在 C 中匹配?
- c# - Cancel all Task.Run executing particular method
- javascript - 在嵌套数组中查找项目的索引
- bonita - 如何使用表单的一个字段填充 Bonita 中的其他字段?
- html - 如何使用 curl 手动登录 HG 路由器?
- google-chrome - 为什么在 --disable-web-security 和删除 x-frame-options 标头之后仍然显示此错误?
- java - 休眠:哪一列???将数据类型 nvarchar 转换为数值时出错
- r - 生成与现有数据相关的随机序数变量