亲爱的神秘黑客和传奇程序员:
我已经知道,扫描器用于识别用户输入,就像虚拟java手册的io部分中的java bufferedreader一样。问题是,如何以最简单的术语应用扫描仪,即从设置按钮开始,例如{A}或{1}?我如何应用扫描器让java意识到这些按钮被按下了,如果是,那么打印设置的条件?
逻辑:扫描仪应用程序??...(我需要这方面的帮助)
如果为A,则为System.out.println("Hi, my name is A!!")
如果为1,则为System.out.println("Hi, my name is 1!!")
非常感谢
发布于 2014-02-27 04:12:19
据我所知(有些有限),在按下enter (返回)键之前,读取System.in的扫描仪不会传递任何数据(因为在此之前,它没有要传递的数据),因此它不会对单独的按钮按下做出响应。如果您按enter键,它将会响应,因此请使用类似以下内容:
Scanner yourScanner = new Scanner(System.in);
//Creates a scanner that reads from the terminal.
System.out.println("What is my name? ");
//Whatever you want the user to be asked, let them know what to input.
String theirResponse = scan.next();
//Or .nextLine() if the input contains a space in the middle
System.out.println("Hi, my name is " + theirResponse + "!!");
//Prints out and uses their response.不幸的是,Scanner不是一个很好的关键监听方式。
https://stackoverflow.com/questions/14223592
复制相似问题