【问题标题】:making commands with java and user input使用 java 和用户输入制作命令
【发布时间】:2020-09-15 01:57:14
【问题描述】:

我正在制作一个电影商店程序,但我被困在如何制作诸如购买电影或信息电影之类的命令

import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;

public class Pricing {

     // TEST
static double Cost;
String DieHard_Name;
String DieHard_Info;
static double DieHard_Price = 6.99;
static boolean DieHard_incart;
// Test

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");
            Genre.add("Documentary");

            System.out.println("Welcome to the movie store Please type the genre of movie you would like to"
                    + " browse");  
            System.out.println("Type 'Help' in the console for assistance");
            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 for Documentaries
    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");

    ArrayList<String> Help_pls = new ArrayList<String>();
    Help_pls.add("");



     // If the input doesn't match one of the options a "please try again message appears"

     String chooseGenre;
     Boolean isProgramRunning = true;

     while (isProgramRunning) {

         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); */

         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 "Help" :
                  System.out.println("You chose: Help");
                  Help_pls.forEach(System.out::println);

                  // THIS HERE
              case "Buy Die hard " :
                  if (DieHard_incart = false) {
                     System.out.println("Selected buy: Die hard "); 
                     Cost = 0 + DieHard_Price;
                     DieHard_incart = true ;
                  } else {
                      System.out.println("This movie is allready in your cart");
                  }
                     break;




              case "Exit":
                  isProgramRunning = false;
                  System.out.println("Program terminated");
                  break;
              default :
                 System.out.println("Please enter a Movie genre or usable command");
                 break;
           }

     }//ndloop

} //end 

  }

当我用力输入 Buy Die 时,它​​给了我三遍“请输入电影类型或可用命令” 我不知道是否一直使用 switch 语句会起作用,或者它是否会使我的程序运行缓慢

有什么方法可以解决这个问题或更简单的方法吗?

【问题讨论】:

    标签: java command java.util.scanner


    【解决方案1】:

    如果要解析带有空格的文本,必须将chooseGenre = scan.next() 更改为chooseGenre = scan.nextLine()

    “请输入电影类型或可用命令”显示 3 次,因为您发送到应用程序的命令由 3 个单词组成,由 2 个空格分隔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-13
      • 2020-02-12
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      相关资源
      最近更新 更多