英文:
How to store a Scanner input and have it correspond with an array? Then pick a random element from that array
问题
在一个初学者的Java项目中工作。
程序的想法是接收用户喜欢的流派,然后根据每个流派中排名前10的书籍(现在从3开始)列表返回一个随机推荐。
我的主要问题是如何接收用户的输入,将其对应到正确的数组,并在getRecommendation()
方法中打印出该数组中的随机元素。
以下是代码部分(省略了导入和公共类部分):
public WhatBook() {
System.out.println("你好,你现在想要哪种类型的书?");
System.out.println("请输入:\n1 代表恐怖\n2 代表冒险\n3 代表犯罪\n4 代表战争\n5 代表科幻");
while (true) {
Scanner scanGenre = new Scanner(System.in);
int bookGenre = scanGenre.nextInt();
if (bookGenre < 1 || bookGenre > 5) {
System.out.println("请选择1到5之间的类型。");
} else {
System.out.println(bookGenre + "? 很好的选择!让我们为您提供一些建议。");
break;
}
}
}
public static String getRecommendation() {
// 随机选择输入类型的数组元素
// 创建随机对象
int rnd = new Random().nextInt(array.length);
return array(rnd);
}
// 公共数组
String[] horror = { "闪灵", "站台", "山屋惊魂" };
String[] adventure = { "勇闯天涯", "斧头", "野性的呼唤" };
String[] crime = { "冷血谋杀", "白城恶魔", "地狱陷阱" };
String[] war = { "他们背负的事情", "西线无战事", "第二十二条军规" };
String[] scifi = { "沙丘", "1984", "安德的游戏" };
public static void main(String[] args) {
WhatBook user1 = new WhatBook();
}
我尝试在构造函数中使用switch循环将每个int值设置为其对应的数组。像这样:
switch (bookGenre) {
case 1:
bookGenre = horror[0];
break;
// ...
}
但这并不起作用。有没有关于如何存储bookGenre int值以匹配其数组的想法呢?
英文:
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?
答案1
得分: 0
你正在寻找关于 字段(a field) 的概念。
英文:
you're looking for the concept of a field.
答案2
得分: 0
我将从现有的书籍数组中创建一个数组或数组(二维数组):
String[][] genres = { horror, adventure, crime, war, scifi };
只需确保它们按照与菜单系统相同的顺序列出。由于您的菜单从1开始,我们需要从用户选择中减去1,以使其从零开始,与数组一致。
接下来,我会将您的getRecommendation()
方法更改为接收一个数组。此外,我会声明一个可以重复使用的静态Random
实例,而不是每次都创建一个新的实例:
private static Random R = new Random();
public static String getRecommendation(String[] array) {
// 从输入的类别数组中随机选择元素
// 创建随机对象
int rnd = R.nextInt(array.length);
return array[rnd];
}
最后,只需确保用户输入的数字在数组的范围内,并将相应的数组传递给方法:
int bookGenre = scanGenre.nextInt();
if (bookGenre >= 1 && bookGenre <= genres.length) { // 在1和genres.length之间,以匹配显示的菜单
System.out.println(bookGenre + "?好选择!让我们来一些推荐。");
String book = getRecommendation(genres[bookGenre - 1]); // 减去1使其从零开始
System.out.println("您的推荐书籍:" + book);
} else {
System.out.println("无效的选择。");
}
这是整体代码:
import java.util.Random;
import java.util.Scanner;
public class WhatBook {
private static Random R = new Random();
// 公共数组
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" };
String[][] genres = { horror, adventure, crime, war, scifi };
public WhatBook() {
System.out.println("你好,你想看什么类型的书?");
System.out.println("请输入:\n1 代表恐怖\n2 代表冒险\n3 代表犯罪\n4 代表战争\n5 代表科幻\n");
Scanner scanGenre = new Scanner(System.in);
System.out.print("你的选择:");
int bookGenre = scanGenre.nextInt();
if (bookGenre >= 1 && bookGenre <= genres.length) {
System.out.println(bookGenre + "?好选择!让我们来一些推荐。");
String book = getRecommendation(genres[bookGenre - 1]);
System.out.println("你的推荐书籍:" + book);
} else {
System.out.println("无效的选择。");
}
}
public static String getRecommendation(String[] array) {
// 从输入的类别数组中随机选择元素
// 创建随机对象
int rnd = R.nextInt(array.length);
return array[rnd];
}
}
注意:上面的翻译可能会因为格式等细微差异而不完全一致,但应该能准确表达相同的意思。
英文:
I would make an Array or Arrays (a 2D array), from your existing book arrays:
String[][] genres = { horror, adventure, crime, war, scifi };
Just make sure that they are listed in the same order as your menu system. Since your menu starts at 1, we'll have to subtract 1 from the user choice to make zero based like the array.
Next, I would change your getRecommendation()
method to receive an array. Additionally, I would declare a static instance of Random
that can be re-used over and over, instead of creating a new one each time:
private static Random R = new Random();
public static String getRecommendation(String[] array) {
// pick random element of the entered genre array
// create random object
int rnd = R.nextInt(array.length);
return array[rnd];
}
Lastly, just make sure that the number the user entered is within the bounds of your array, and pass the corresponding array to the method:
int bookGenre = scanGenre.nextInt();
if (bookGenre >=1 && bookGenre <= genres.length) { // between 1 and genres.length to match the displayed menu
System.out.println(bookGenre + "? Great choice! Let's get some recommendations.");
String book = getRecommendation(genres[bookGenre - 1]); // subtract 1 to make it zero based
System.out.println("Your recommendation: " + book);
}
else
{
System.out.println("Invalid Choice.");
}
Here's the whole thing put together:
import java.util.Random;
import java.util.Scanner;
public class WhatBook {
private static Random R = new Random();
// 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" };
String[][] genres = { horror, adventure, crime, war, scifi };
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\n");
Scanner scanGenre = new Scanner(System.in);
System.out.print("Your choice: ");
int bookGenre = scanGenre.nextInt();
if (bookGenre >=1 && bookGenre <= genres.length) {
System.out.println(bookGenre + "? Great choice! Let's get some recommendations.");
String book = getRecommendation(genres[bookGenre - 1]);
System.out.println("Your recommendation: " + book);
}
else
{
System.out.println("Invalid Choice.");
}
}
public static String getRecommendation(String[] array) {
// pick random element of the entered genre array
// create random object
int rnd = R.nextInt(array.length);
return array[rnd];
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论