java - 在 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 中没有发生?
谢谢你的帮助。
解决方案
推荐阅读
- rust - 性能分析
- python - 如何在 Django 模板语言中进行算术运算后将值重新分配给变量
- python - Unable to import tensorflow2.0,(no gpU) ImportError: DLL load failed: 找不到指定的程序
- angular - 在序列化数据中,角度将未选中的复选框设置为 false
- python - 使用 Python 和 PyAudio 的语音转文本无法在操作系统上运行
- php - 巴士时刻表 - API wmata - PHP
- rust - 可变的可选输出引脚
- sql - upsert 冲突目标中的 Where 子句
- django - 如何在 Cloud Foundry 中为 python buildpack 升级 pip 版本
- routes - Zend Framework 3 Route Segment 参数问题