多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Java 程序:从标准输入读取整数值 > 原文: [https://beginnersbook.com/2017/09/java-program-to-read-integer-value-from-the-standard-input/](https://beginnersbook.com/2017/09/java-program-to-read-integer-value-from-the-standard-input/) 在这个程序中,我们将看到如何读取用户输入的整数。`Scanner`类位于`java.util`包中。它用于捕获原始类型的输入,如`int`,`double`等和字符串。 ## 示例:用于读取用户输入的数字的程序 我们已导入包`java.util.Scanner`以使用`Scanner`。为了读取用户提供的输入,我们首先通过传递`System.in`作为参数来创建`Scanner`的对象。然后我们使用`Scanner`类的`nextInt()`方法来读取整数。如果您不熟悉 Java 并且不熟悉 java 程序的基础知识,那么请阅读[核心 Java](https://beginnersbook.com/java-tutorial-for-beginners-with-examples/) 的以下主题: + [编写您的第一个 Java 程序](https://beginnersbook.com/2013/05/first-java-program/) + [JVM 如何工作](https://beginnersbook.com/2013/05/jvm/) ```java import java.util.Scanner; public class Demo { public static void main(String[] args) { /* This reads the input provided by user * using keyboard */ Scanner scan = new Scanner(System.in); System.out.print("Enter any number: "); // This method reads the number provided using keyboard int num = scan.nextInt(); // Closing Scanner after the use scan.close(); // Displaying the number System.out.println("The number entered by user: "+num); } } ``` **输出:** ```java Enter any number: 101 The number entered by user: 101 ```