在读取int并在java中读取int后,请致电NextLine()()需要三次阅读字符串
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine(); // gets rid of the pesky newline
String s = scan.nextLine();
scan.close();
/* Print output */
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
上面的代码在Hackerrank编译器中效果很好。如果我在Intellij上运行它,则需要额外调用scan.nextline()才能读取实际字符串。
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine(); // gets rid of the pesky newline
scan.nextLine(); // gets rid of the pesky newline
String s = scan.nextLine();
scan.close();
/* Print output */
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
有人可以帮助我为什么会发生这种情况吗?我假设对NextDouble()的调用将忽略NextInt()在缓冲区中留下的\ n,而是将下一个双令牌拾取,并在缓冲区中留下一个新的\ n。因此,对NextLine()的单个呼叫应该足以清除缓冲区的\ n,但是当我在Intellij上运行此时需要2个调用。
这是Java版本问题,还是我缺少的真正基本内容?
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine(); // gets rid of the pesky newline
String s = scan.nextLine();
scan.close();
/* Print output */
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
The above code works perfectly fine in hackerrank compiler. While if i run it on IntelliJ, one extra call to scan.nextLine() is required in order to read the actual string.
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
/* Read input */
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine(); // gets rid of the pesky newline
scan.nextLine(); // gets rid of the pesky newline
String s = scan.nextLine();
scan.close();
/* Print output */
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Can someone help me with why this could happen? I am assuming the call to nextDouble() would ignore the \n left in the buffer by nextInt() and pickup the next double token and leave a new \n in the buffer. So a single call to nextLine() should be enough to clear that \n from buffer, but why 2 calls are needed when I run this on IntelliJ.
Is this a JAVA version issue or is there something really basic which I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会看到这个官方想法问题的效果: Console ReadLine Skips Input in 2022.1.1
您可以更新到2022.1.2修复的位置。
You probably see the effect of this official IDEA issue: Console readLine skips input in 2022.1.1
You could update to 2022.1.2 where this is fixed.