【问题标题】:How to exit the program when Ctrl+D is entered in Java?Java中输入Ctrl+D如何退出程序?
【发布时间】:2017-09-17 04:10:55
【问题描述】:

下面是我的逆波兰计算器的一部分。

如果输入了一个整数,则将其压入堆栈,如果按下 =,则查看结果。但是,我想添加另一个条件:如果用户按下 CTRL + D,则程序退出。

我在网上查了一下,但似乎找不到任何解决方案。有任何想法吗?谢谢。

Scanner mySc = new Scanner(System.in);
//If input is an integer, push onto stack.
 if (mySc.hasNextInt()) {       
    myStack.push(mySc.nextInt());
} 
//Else if the input is an operator or an undefined input. 
else if (mySc.hasNext()) {
    //Convert input into a string.
    String input = mySc.nextLine(); 
    //Read in the char at the start of the string to operator.
    char operator = input.charAt(0); 
    if (operator == '=') {
        //Display result if the user has entered =.
    }
**else if ("CTRL-D entered") {
    System.exit(0);
    }**

【问题讨论】:

  • 使用键绑定。您将无法使用扫描仪进行操作。
  • 等待:你想让用户写 CTRL-D 还是按下按钮?
  • @PhilippSander 按下按钮
  • 在这种情况下,扫描仪将无济于事。使用键绑定
  • 好的,我得查一下。使用缓冲阅读器怎么样?有没有办法使用它?

标签: java null java.util.scanner exit ctrl


【解决方案1】:

试试这个:

public static void main(String[] args) {
    try {
        byte[] b = new byte[1024];
        for (int r; (r = System.in.read(b)) != -1;) {
            String buffer = new String(b, 0, r);
            System.out.println("read: " + buffer);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

在这种情况下,当您按下 CTRL+D 时,循环将停止,这是因为 CTRL+DSystem.in InputStream 发送EOF 信号,即-1。在 *nix 系统上就是这种情况,对于 Windows 系统,EOF 信号是使用 CTRL+Z 组合键发送的

【讨论】:

    【解决方案2】:

    试试这个代码。它实际上对我有用

    // press ctrl+Z(windows) and ctrl+D(mac, linux) for input termination
    StringBuilder out = new StringBuilder();
    String text = null;
    Scanner scanner = new Scanner( System.in );
    while( scanner.hasNextLine() )
    {
        text = new String( scanner.nextLine() );
        out.append( text );
    }
    scanner.close();
    System.out.println( out );
    System.out.println( "program terminated" );
    

    【讨论】:

      【解决方案3】:

      我假设您说的是控制台应用程序?

      在 unix 系统上,按 Ctrl+D 会关闭应用程序的标准输入。 这将导致Scanner 下的输入流关闭,这将导致mySc.hasNext() 返回false。

      在windows上,用户需要按Ctrl+Z 回车效果相同。

      如果输入流尚未关闭,则mySc.hasNext() 将阻塞,直到它可以返回true

      所以你需要将你的程序包装在一个 while(mySc.hasNext()) 循环中,例如

          public static void main(String[] args) {
      
              Scanner mySc = new Scanner(System.in);
      
              while(mySc.hasNext()) {
      
      //If input is an integer, push onto stack.
                  if (mySc.hasNextInt()) {
                      System.out.println(String.format("myStack.push(%s);", mySc.nextInt()));
                  }
      //Else if the input is an operator or an undefined input.
                  else {
                      //Convert input into a string.
                      String input = mySc.nextLine();
                      System.out.println(String.format("input = %s", input));
      
                      //Read in the char at the start of the string to operator.
      //                char operator = input.charAt(0);
      //                if (operator == '=') {
      //                    //Display result if the user has entered =.
      //                }
      //            **else if ("CTRL-D entered") {
      //                System.exit(0);
      //            }**
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 2023-04-10
        相关资源
        最近更新 更多