【问题标题】:How do i make a scanner so that I'm always able to input commands如何制作扫描仪,以便始终能够输入命令
【发布时间】: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");
      }

 } 



  }

我并不完全擅长编程,但我会寻求任何可以得到的帮助

【问题讨论】:

标签: java java.util.scanner


【解决方案1】:

您可以为此使用 while 循环。


Boolean isProgramRunning = true;
String chooseGenre;

while (isProgramRunning) {
    chooseGenre = scan.next();

    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);
             break;
         case "Exit":
             isProgramRunning = false;
             break;
         default :
            System.out.println("Please enter a Movie genre");
      }
}

我想补充几点: 我建议创建一个具有流派名称字符串和 ArrayList 属性的流派类。这样你就可以清楚地知道你有多少类型并且可以更清楚地看到这些列表与各自的类型有关。 您还阻止了用户交互 3 秒,我真的没有看到这样做的目的。如果在后台进行大量计算或排序,而用户看不到,则“加载”通知是有意义的。您还忘记了开关的最后一种情况下的中断。这意味着,如果您选择纪录片,它也将落入默认情况。

【讨论】:

    猜你喜欢
    • 2020-08-26
    • 2016-06-20
    • 2016-06-12
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2014-06-05
    • 2021-11-02
    相关资源
    最近更新 更多