在读取int并在java中读取int后,请致电NextLine()()需要三次阅读字符串

发布于 2025-02-05 10:29:07 字数 1470 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一萌ing 2025-02-12 10:29:07

您可能会看到这个官方想法问题的效果: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文