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



从事初级java项目。

该程序的想法是接受用户最喜欢的类型,并根据每种类型的前10名(现在从3名开始(图书列表返回随机推荐。

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

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

public WhatBook() {

System.out.println("Hello, what genre of book are you in the mood for?");
System.out.println("Please enter: n1 for Horrorn2 for Adventuren3 for Crimen4 for Warn5 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();
}

我尝试在构造函数中使用switch循环,将每个int值设置为相应的数组。类似:

开关(图书类型({

情况1:bookGenre=恐怖[0];打破等等。

但这行不通。

有什么想法如何存储bookGenre int值以匹配其数组吗?

我会从你现有的书籍数组中制作一个或多个数组(2D数组(:

String[][] genres = { horror, adventure, crime, war, scifi };

只要确保它们按照与菜单系统相同的顺序列出即可。由于您的菜单从1开始,我们必须从用户选择中减去1,以使其像数组一样基于零。

接下来,我将更改您的getRecommendation()方法以接收数组。此外,我会声明一个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.");
}

以下是全部内容:

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 Horrorn2 for Adventuren3 for Crimen4 for Warn5 for Scifin");
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];
}
}

您正在寻找字段的概念。

相关内容

最新更新