Working on a beginner java project.
The idea of the program is to take in user's favorite genre and return a random recommendation based on a top 10 (starting with 3 now) list of books in each genre.
My main question is how could I take in their input, make it correspond to the right array and print out a random element from that array in the getRecommendation() method.
Code below (left out imports and public class):
public WhatBook() {
System.out.println("Hello, what genre of book are you in the mood for?");
System.out.println("Please enter: \n1 for Horror\n2 for Adventure\n3 for Crime\n4 for War\n5 for Scifi");
while (true) {
Scanner scanGenre = new Scanner(System.in);
int bookGenre = scanGenre.nextInt();
if (bookGenre < 1 || bookGenre > 5) {
System.out.println("Please choose from the genres from 1 to 5.");
} else {
System.out.println(bookGenre + "? Great choice! Let's get some recommendations for you.");
break;
}
}
public static String getRecommendation() {
// pick random element of the entered genre array
// create random object
int rnd = new Random().nextInt(array.length);
return array(rnd);
}
// public arrays
String[] horror = { "The Shining", "The Stand", "The Haunting of Hill House" };
String[] adventure = { "Into Thin Air", "Hatchet", "The Call of the Wild" };
String[] crime = { "In Cold Blood", "Devil in the White City", "Helter Skelter" };
String[] war = { "The Things They Carried", "All Quiet on the Western Front", "Catch-22" };
String[] scifi = { "Dune", "1984", "Ender's Game" };
public static void main(String[] args) {
WhatBook user1 = new WhatBook();
}
I tried using a switch loop in the constructor to set each int value to its corresponding array. Like:
switch (bookGenre) {
case 1: bookGenre = horror[0]; break; etc...
but this doesn't work.
Any ideas how to store the bookGenre int value to match its array?
Aucun commentaire:
Enregistrer un commentaire