Java,switch语句:在同一行写命令和文本消息+对象名作为命令?

Java, switch statement: Write command and text message on the same line + object name as a command?

我是 Java 的初学者。在我的程序中,用户应该在按下 return-键,程序应该打印输入的文本。这可能吗?我的目标是这样的:

Command> message this is a message!
this is a message!

目前我正在为程序中的所有命令使用 switch 语句。现在我必须写 "message" 然后在写文本之前按回车键。

 Command> message
 this is a message!
 this is a message! (output)

我的代码:

switch (cmd) {
    //other commands 

      case "message":
                printMessage();
                break;
      default:
        System.out.println("Wrong command!");

}

public void printMessage() {
        String text = keyboard.nextLine();
        System.out.println(text);
    }

我还必须在程序中包含另一个命令,用户在其中输入事件的名称,程序会打印有关该事件的信息。 是否可以将对象名称作为命令?

Command> event name
information about event above, if the event exists (output)

一个 switch 语句似乎太局限了?如果那行不通,我还有什么其他选择?

我假设您有 class 并且您至少可以在终端中打印一条 HelloWorld 消息

所以...您将需要:

Scanner 对象,以便您可以读取用户提供的输入和字符串中的 Split 方法 class,因此您可以 split 输入 message Hola_World 分为两部分,第一部分是 message 第二部分是 Hola_World

示例:

public static void main (String[] args){
    Scanner inputScanner = new Scanner(System.in);
    String userInput = inputScanner.nextLine();
    System.out.println("Foo: " + userInput);
    String[] userInputSplitted = userInput.split(" " );
    String fisrtPart = userInputSplitted[0];
    String secondPart = userInputSplitted[1];
    System.out.println("1st Part: " + fisrtPart);
}