【问题标题】:Using boolean to print invalid commands in Java在 Java 中使用布尔值打印无效命令
【发布时间】:2018-05-09 10:47:08
【问题描述】:

我正在尝试构建自己的命令行界面。

最令人困惑的部分是根据用户输入打印出无效的命令。

我尝试使用布尔函数来检查每个命令的有效性。

即使我的命令无效,我也希望我的程序继续运行。

退出while循环后如何再次进入?

String command;
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
boolean R = true;

while (R==true) {
    System.out.print("Command: ");
    command = console.readLine();

    R = false;

    ArrayList commandParams = new ArrayList();
    String[] commandSplit = command.split(" ");

    if (commandSplit.length > 1) {
        for (int i = 1; i < commandSplit.length; i++) {
            commandParams.add(commandSplit[i]);
        }
    }

    if (commandSplit[0].equals("time")) {
        Date time = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss ");
        System.out.println(sdf.format(time));
        R = true;
    }

    // Print today's date.
    if (commandSplit[0].equals("date")) {
        Date date = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat(" YYYY/MM/DD");
        System.out.println(sdf.format(date));
        R = true;
    }

    // Exit the console when "exit" is entered.
    if (commandSplit[0].equals("exit")) {
        System.exit(0);
        R = true;
    }
}
System.out.println("Invalid command");

System.out.print("Command: ");
command = console.readLine();

【问题讨论】:

  • 你快到了,你所要做的就是删除 R=false;并仅在用户决定结束会话时将此值设置为 false。你的一个问题是为什么你有 command = console.readLine();在程序结束时,我错过了这个。

标签: java boolean


【解决方案1】:

将无效命令的代码移到循环内,即

while (R==true) {


        System.out.print("command: ");


        command = console.readLine();
        R=false;

       ...

        //Exit the console when "exit" is entered.
        if (commandSplit[0].equals("exit")) {
            System.exit(0);
            R=true;
        }

        if(R==false){
            System.out.println("invalid command");
            R=true;
        }       
    }              
}

【讨论】:

  • 用户想退出控制台时进入exit。但是由于设置了布尔标志(R),程序永远不会终止,对吧?
  • @User27854 如果命令等于“exit”,他会调用System.exit(0),因此程序将终止。
  • 我错过了,抱歉 :)
【解决方案2】:

你可以做这样的事情。你甚至不需要布尔变量。

while (true) {
    String command = console.readLine();
    if (command.equals("date")) {
        //print date
        continue;
    }
    if (command.equals("time")) {
        //print time
        continue;
    }
    if (command.equals("exit")) {
        break;
    }
    System.out.println("invalid command");        
    command = console.readLine();
}

【讨论】:

    【解决方案3】:

    具体的答案:你无法重蹈覆辙。

    但是想一想:你真的需要每次都将布尔变量设置为 false 吗?不,只有在您想终止程序时才需要它。

    您可以使用很多 if-else 语句,但从 JDK 7 开始,我们终于能够在完全符合您需求的 switch-case 结构中比较字符串:

    boolean loop = true;
    while ( loop )
    {
      // Your read and parse functions go here...
      switch ( commandSplit[ 0 ] )
      {
        case "exit":
          loop = false;
          break;
        case "time":
          // dummy method
          printTime();
          break;
        // default will be executed if none of the above cases is met.
        default:
          System.out.println( "Invalid / Unknown command." );
          break;
    
      }
    }
    // shutdown logic goes here (if needed).
    

    这样做可以让您轻松扩展程序的功能,因为如果您想实现新命令,您可以轻松添加新案例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-02
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 2019-07-31
      相关资源
      最近更新 更多