本文介绍了Java使用扫描仪输入密钥按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java编程。

我正在尝试编写代码,可以识别用户是否在基于控制台的程序中按下回车键。

I am programming using Java.
I am trying write code which can recognize if the user presses the enter key in a console based program.

如何使用java执行此操作。有人告诉我,这可以使用Scanner或缓冲输入阅读器完成。我不明白(或知道如何使用)缓冲输入阅读器。

How can I do this using java. I have been told that this can be done using either Scanner or, buffered input reader. I do not understand(or know how to use) buffered input reader.

我试图用扫描仪做这个,但是按两次后输入程序终止,它没有工作

I tried to do do this using scanner but after pressing enter twice the program terminates, and it doesn't work

    Scanner readinput = new Scanner(System.in);

    String enterkey = "Hola";
    System.out.print(enterkey);


    enterkey = readinput.nextLine();
     System.out.print(enterkey);

    if(enterkey == ""){

        System.out.println("It works!");

谢谢

- 编辑 -
以下代码使用等于方法为字符串而不是 ==

-- edit --the following code works using the equals method for the string instead of ==

    Scanner readinput = new Scanner(System.in);

    String enterkey = "Hola";
    System.out.print(enterkey);


    enterkey = readinput.nextLine();
     System.out.print(enterkey);

    if(enterkey.equals("")){

        System.out.println("It works!");

如何做到这一点,以及使用缓冲输入阅读器进行此操作的优点是什么?

how can this be done, and what are the pros to doing this using the buffered input reader?

推荐答案

这可以使用java.util.Scanner,并将进行多次输入击键:

This works using java.util.Scanner and will take multiple "enter" keystrokes:

    Scanner scanner = new Scanner(System.in);
    String readString = scanner.nextLine();
    while(readString!=null) {
        System.out.println(readString);

        if (readString.isEmpty()) {
            System.out.println("Read Enter Key.");
        }

        if (scanner.hasNextLine()) {
            readString = scanner.nextLine();
        } else {
            readString = null;
        }
    }

将其分解:

Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();

这些行初始化一个新的扫描器即阅读(键盘)并读取一行从它开始。

These lines initialize a new Scanner that is reading from the standard input stream (the keyboard) and reads a single line from it.

    while(readString!=null) {
        System.out.println(readString);

当扫描仪仍然返回非空数据时,将每行打印到屏幕上。

While the scanner is still returning non-null data, print each line to the screen.

        if (readString.isEmpty()) {
            System.out.println("Read Enter Key.");
        }

如果输入(或返回,或其他)键由输入, nextLine()方法将返回一个空字符串;通过检查字符串是否为空,我们可以确定是否按下了该键。这里会打印读取输入密钥文本,但您可以在此处执行任何操作。

If the "enter" (or return, or whatever) key is supplied by the input, the nextLine() method will return an empty string; by checking to see if the string is empty, we can determine whether that key was pressed. Here the text Read Enter Key is printed, but you could perform whatever action you want here.

        if (scanner.hasNextLine()) {
            readString = scanner.nextLine();
        } else {
            readString = null;
        }

最后,打印内容和/或在输入时执行某些操作按下键,我们检查扫描仪是否有另一条线;对于标准输入流,此方法将阻塞,直到流关闭,程序执行结束或提供进一步输入。

Finally, after printing the content and/or doing something when the "enter" key is pressed, we check to see if the scanner has another line; for the standard input stream, this method will "block" until either the stream is closed, the execution of the program ends, or further input is supplied.

这篇关于Java使用扫描仪输入密钥按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:48