在java中难以读取整数?它背后有什么理由吗? [英] Hard to read integers in java? Is there any reason behind it?
问题描述
我必须在Java中更频繁地读取整数。那我该怎么办?我必须使用什么功能?
I have to read integers more frequently in Java. So how i should do? What function i have to use?
C / C ++也有缓冲概念,但它们只有一个语句。但是在Java中我们必须调用至少2个函数来读取整数。
C/C++ too have buffering concepts but they has a single statement. But in Java we have to call at least 2 function to read an integer.
例如,
要从控制台读取整数,
我将使用
-
scanf
in C -
cin
in C ++
scanf
in Ccin
in C++
但在Java中,
BufferedReader bufferedreader = new BufferedReader(
new InputStreamReader(System.in)); String number = bufferedreader.readLine();
int value = Integer.parseInt(number);
或
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
有人请解释一下:我能否拥有比更简单的版本? cin
或 scanf
(单行声明)。
Someone please explain me: Can I have a simpler version than these similar to cin
or scanf
(A one line statement).
提前致谢。
推荐答案
我相信 Scanner
提供最简单的界面。我不相信标准API提供了更简单的方法。 (你当然可以将片段封装在一个方法中,然后调用该方法。)
I believe Scanner
provides the simplest interface. I don't believe there is a simpler approach provided by the standard API. (You could of course encapsulate the snippet in a method, and call the method instead.)
我能想出的唯一一个明显的
The only one-liner I can come up with is the obvious
int i = new Scanner(System.in).nextInt();
这篇关于在java中难以读取整数?它背后有什么理由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!