首页 > 解决方案 > 在 Intellij 之外运行代码时输入流中的额外值

问题描述

所以我想看看流在java中是如何工作的,我写了一些代码,它应该采用2个一位整数并返回它们的总和。

这是 intellij 中的工作代码:

public class practice {
public static void main(String[] args) throws IOException {
    int x = Character.getNumericValue((char)System.in.read());
    System.in.read(); // reads the -1 so that input stream is now empty
    int y = Character.getNumericValue((char)System.in.read());

    System.out.println(x+y);

}}

这是intellij之外的工作代码,在eclipse或其他任何地方:

public class practice {
public static void main(String[] args) throws IOException {
    int x = Character.getNumericValue((char)System.in.read());
    System.in.read();
    System.in.read();
    int y = Character.getNumericValue((char)System.in.read());

    System.out.println(x+y);

}}

我不得不添加一个额外的 System.in.read() 因为不知何故在流中有另一个值,为了检查这一点,我将该值打印到控制台并将其值为 13,我将其与“回车”链接" 来自 ASCII 表,什么是回车,为什么这在 intellij 中没有发生?

谢谢你的帮助。

标签: javaintellij-ideasystem.in

解决方案


推荐阅读