Java 如何存储扫描仪输入并使其与阵列相对应?然后从该数组中选择一个随机元素

Java 如何存储扫描仪输入并使其与阵列相对应?然后从该数组中选择一个随机元素,java,arrays,random,methods,constructor,Java,Arrays,Random,Methods,Constructor,从事一个java初学者项目 这个程序的想法是接受用户最喜欢的体裁,并根据每个体裁的前10名(从现在的3名开始)书籍列表返回随机推荐 我的主要问题是如何接收他们的输入,使其对应于正确的数组,并在getRecommension()方法中从该数组中打印出一个随机元素 下面的代码(省略了导入和公共类): public WhatBook(){ System.out.println(“你好,你想看哪种类型的书?”); System.out.println(“请输入:\n1表示恐怖\n2表示冒险\n3表示犯罪

从事一个java初学者项目

这个程序的想法是接受用户最喜欢的体裁,并根据每个体裁的前10名(从现在的3名开始)书籍列表返回随机推荐

我的主要问题是如何接收他们的输入,使其对应于正确的数组,并在getRecommension()方法中从该数组中打印出一个随机元素

下面的代码(省略了导入和公共类):

public WhatBook(){
System.out.println(“你好,你想看哪种类型的书?”);
System.out.println(“请输入:\n1表示恐怖\n2表示冒险\n3表示犯罪\n4表示战争\n5表示科幻”);
while(true){
Scanner scangree=新扫描仪(System.in);
int bookgreen=scangree.nextInt();
如果(图书类型<1 | |图书类型>5){
System.out.println(“请从1到5的类型中选择”);
}否则{
System.out.println(bookquare+“?不错的选择!让我们为您提供一些建议。”);
打破
}
}
公共静态字符串getRecommension(){
//选择输入的类型数组的随机元素
//创建随机对象
int rnd=new Random().nextInt(array.length);
返回阵列(rnd);
}
//公共阵列
字符串[]恐怖={“闪光”,“看台”,“山屋闹鬼”};
String[]冒险={“飞入稀薄的空气”、“斧头”、“野性的呼唤”};
字符串[]犯罪={“冷血中的”、“白城中的魔鬼”、“乱哄哄的”};
String[]war={“他们所携带的东西”,“西线一切平静”,“第22条军规”};
字符串[]科幻={“沙丘”,“1984”,“安德的游戏”};
公共静态void main(字符串[]args){
WhatBook user1=新WhatBook();
}
我尝试在构造函数中使用开关循环将每个int值设置为其对应的数组。例如:

切换(图书类型){

案例1: 书籍类型=恐怖[0]; 打破 等等

但这不起作用


您知道如何存储BookGene int值以匹配其数组吗?

您正在寻找的概念。

我将从您现有的书籍数组中创建一个或多个数组(2D数组):

String[][] genres = { horror, adventure, crime, war, scifi };
只要确保它们以与菜单系统相同的顺序列出。由于菜单从1开始,我们必须从用户选择中减去1,使其像数组一样以零为基础

接下来,我将更改您的
getRecommension()
方法以接收数组。此外,我将声明可以反复使用的
Random
的静态实例,而不是每次创建一个新实例:

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];
}
最后,只需确保用户输入的数字在数组的范围内,并将相应的数组传递给方法:

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.");
}
int bookGenre=scangree.nextInt();

如果(bookGenre>=1&&bookGenre好的,谢谢。我将尝试思考答案。如果我将bookGenre设置为公共字段,我的方向是否正确?因此getRecommension()方法可以访问它?更高级的解决方案将使用a,其中genre作为键,字符串数组作为值。
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.");
}
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];
  }

}