如何从Java中的标准输入读取整数值 [英] How to read integer value from the standard input in Java
本文介绍了如何从Java中的标准输入读取整数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用哪个类来读取Java中的整数变量?
What class can I use for reading an integer variable in Java?
推荐答案
您可以使用 java.util.Scanner
( API ):
import java.util.Scanner;
//...
Scanner in = new Scanner(System.in);
int num = in.nextInt();
它还可以用正则表达式等标记输入.API有示例,还有很多其他的这个网站(例如如果输入错误的类型,如何防止扫描程序抛出异常?。
It can also tokenize input with regular expression, etc. The API has examples and there are many others in this site (e.g. How do I keep a scanner from throwing exceptions when the wrong type is entered?).
这篇关于如何从Java中的标准输入读取整数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文