I'm pretty new to Java and in an effort to learn more I'm trying to make a game whereby you're given a random lyric from a song, and then the song title and artist.
This would be pretty easy, however I wanted the lyric to be random, and then print the artist and song title using a seperate method. My problem is that I'm not sure how to go from printing one random string to printing a specific answer.
I currently have the lyrics and the answers in a hashmap with the lyrics as keys and answers as values, and can print random keys by turning them into an arraylist using this method
public void randomLyrics()
{
ArrayList<String> lyricKey = new ArrayList<String>(lyrics.keySet());
if(lyrics.size() > 0) {
Random rand = new Random();
int index = rand.nextInt(lyrics.size());
System.out.println(lyricKey.get(rand.nextInt(lyrics.size())));
}
}
however I'm pretty sure that this isn't the right way to go about it, and am not sure how I would go about printing the answers seperately.
Any help would be much appreciated, also this is my first post so apologies if formatting is off.
Also just to clarify, I want to call a method (randomLyrics) which will display a random lyric such as "that's me in the corner, that's me in the spotlight" and then call a seperate method (answer) which would then print "Losing My Religion - R.E.M."
Many thanks
Aucun commentaire:
Enregistrer un commentaire