【发布时间】:2020-05-10 19:21:18
【问题描述】:
我正在制作一个电影商店程序,当我输入第一个命令时它运行良好,但它不允许我再输入命令。
我认为这与 String ChooseGenre=scan.next() 有关,但如果我删除它,我的 switch 语句将不起作用,因为它不接受扫描仪变量。
我想要它,以便我可以输入一个命令然后能够输入另一个命令。 例如:我输入“动作”,它给了我一个动作电影列表,然后输入“恐怖”,输出将是恐怖电影的列表。
这是我的代码
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
public class oiug {
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) throws InterruptedException{
// Start of the program
ArrayList<String> Genre = new ArrayList<String>();
Genre.add("Action");
Genre.add("Horror");
System.out.println("Welcome to the movie store Please type the genre of movie you would like to"
+ " browse");
Genre.forEach(System.out::println);
// ArrayList for the action movies
ArrayList<String> Action = new ArrayList<String>();
Action.add("Die Hard");
Action.add("Top Gun");
Action.add("Whitehouse Down");
Action.add("The Gentlemen");
Action.add("Mission impossible");
Action.add("John Wick");
Action.add("Atomic Blonde");
Action.add("Inception");
Action.add("The Matrix");
Action.add("Speed");
// ArrayList for the Horror movies
ArrayList<String> Horror = new ArrayList<String>();
Horror.add("Us");
Horror.add("It Chapter 2");
Horror.add("Get Out");
Horror.add("The Interior");
Horror.add("It follows");
Horror.add("The Ring");
Horror.add("The Shinning");
Horror.add("Scream");
Horror.add("Alien");
Horror.add("Nightmare on Elm Street");
ArrayList<String> Documentary = new ArrayList<String>();
Documentary.add("Free Solo");
Documentary.add("13th");
Documentary.add("Fyre");
Documentary.add("Honeyland");
Documentary.add("Strong Island");
Documentary.add("Abducted in plain sight");
Documentary.add("Man on Wire");
Documentary.add("How to Survive a Plague");
String ChooseGenre=scan.next();
System.out.println("Loading.");
TimeUnit.SECONDS.sleep(1);
System.out.println("Loading..");
TimeUnit.SECONDS.sleep(1);
System.out.println("Loading...");
TimeUnit.SECONDS.sleep(1);
// If the input doesn't match one of the options a "please try again messege appears"
switch(ChooseGenre) {
case "Horror" :
System.out.println("you chose: Horror!");
Horror.forEach(System.out::println);
break;
case "Action" :
System.out.println("you chose: Action!");
Action.forEach(System.out::println);
break;
case "Documentary" :
System.out.println("you chose: Documentary!");
Documentary.forEach(System.out::println);
default :
System.out.println("Please enter a Movie genre");
}
}
}
我并不完全擅长编程,但我会寻求任何可以得到的帮助
【问题讨论】:
-
你可以把它包装在一个
while(true)-loop