gpt4 book ai didi

Java扫描仪: stop reading after the first entry

转载 作者:行者123 更新时间:2023-12-01 10:09:03 25 4
gpt4 key购买 nike

我正在寻找一种形式,使扫描仪在您第一次按下时停止读取(因此,如果我按K键,程序会自动考虑我按Intro 键,因此它会停止识别输入,保存 K 并继续执行程序)。

我一开始使用char key= sc.next().charAt(0);,但不知道如何在不按Intro的情况下使其停止 p>

提前致谢!

最佳答案

如果您想在单个特定字符之后停止接受,您应该逐个字符地读取用户的输入。尝试根据 Pattern 进行扫描单个字符或使用Console类。

Scanner scanner = new Scanner(System.in);
Pattern oneChar = new Pattern(".{1}");
// make sure DOTALL is true so you capture the Enter key
String input = scanner.next(oneChar);
StringBuilder allChars = new StringBuilder();

// while input is not the Enter key {
if (input.equals("K")) {
// break out of here
} else {
// add the char to allChars and wait for the next char
}
input = scanner.next(oneChar);
}
// the Enter key or "K" was pressed - process 'allChars'

关于Java扫描仪: stop reading after the first entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36265402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com