在Java中输入Ctrl + D时如何退出程序?

时间:2015-11-26 13:59:17

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

以下是我的反向波兰计算器的一部分。

如果输入整数,则将其推入堆栈,如果按 = ,则查看结果。但是,我想添加另一个条件:如果用户按下 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);
    }**

3 个答案:

答案 0 :(得分:1)

试试这个:

var someLargeNumber = 5000000000;
onmessage = function(e) {
  console.log('Message received from main script');
  for(var i = 0; i < someLargeNumber; i++) {}
  console.log('Posting message back to main script');
  postMessage('done');
}

在这种情况下,按 CTRL + D 时循环将停止,因为 CTRL + D <head> <script> var myWorker = new Worker("worker.js"); function hello() { document.getElementById('hi').innerHTML = "hi"; myWorker.postMessage('test'); console.log('Message posted to worker'); } myWorker.onmessage = function(e) { result.textContent = e.data; console.log('Worker thread is complete'); } </script> 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(); } } EOF发送System.in信号。在* nix系统上就是这种情况,对于Windows系统,使用 CTRL + Z 组合键发送InputStream信号

答案 1 :(得分:0)

我假设您在谈论控制台应用程序?

在unix系统上,按 Ctrl + D 关闭应用程序的stdin。 这将导致@FXML private void mouseHoverInfo() { ImageViewInternalMouseHoverTracker ivimht = new ImageViewInternalMouseHoverTracker(this.imageView); String xPos = ivimht.getX(); String yPos = ivimht.getY(); String colorRed = ivimht.getR(); String colorBlue = ivimht.getG(); String colorGreen = ivimht.getB(); String hexColor = ivimht.getHex(); String pixelInfo = "X: " + xPos + " Y: " + yPos + " | " + "r: " + colorRed + " g: " + colorGreen + " b: " + colorBlue + " | " + hexColor; mainApp.getPixelInfo().setInfoString(pixelInfo); } 下方的输入流关闭,这将导致SELECT A.id, A.name, t.* FROM A LEFT JOIN ( SELECT B.* FROM B LEFT JOIN B b_ ON b.AID = b_.AID AND b.buy_date<b_.buy_date WHERE b_.id IS NULL ) t ON A.id = t.AID WHERE A.name LIKE '%to%' 返回false。

在Windows上,用户需要按 Ctrl + Z 输入才能获得相同的效果。

如果输入流尚未关闭,则Scanner会阻止,直到它返回mySc.hasNext()

因此,您需要将程序包装在mySc.hasNext()循环中,例如

true

答案 2 :(得分:0)

尝试使用此代码。它实际上对我有用

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