简体   繁体   English

Scanner input = new Scanner(System.in) 到底是什么意思?

[英]What does Scanner input = new Scanner(System.in) actually mean?

Scanner input = new Scanner(System.in);

Could you give me a detailed explanation on what the code above is doing step by step?你能详细解释一下上面的代码是做什么的吗? I don't really understand how it works and how it links to me later being able to do this statement:我真的不明白它是如何工作的以及它如何链接到我以后能够做这个声明:

int i = input.nextInt()

Alright, let's elaborate with some simplified explanation about theScanner class.好吧,让我们用一些关于Scanner class 的简化解释来详细说明。

It is a standard Oracle class which you can use by calling the import java.util.Scanner .它是一个标准 Oracle class ,您可以通过调用import java.util.Scanner来使用它。

So let's make a basic example of the class:因此,让我们做一个 class 的基本示例:

class Scanner {
   InputStream source;

   Scanner(InputStream src) {
       this.source = src;
   }

   int nextInt() {
       int nextInteger;
       //Scans the next token of the input as an int from the source.
       return nextInteger;
   }
}

Now when you call Scanner input = new Scanner(System.in);现在当你调用Scanner input = new Scanner(System.in); you make a new object of the Scanner class (so you make a new "Scanner") and you store it in the variable input .您制作了Scanner class 的新 object (因此您制作了一个新的“扫描仪”)并将其存储在变量input中。 At the same time you are calling the (so called) constructor of the class, with the parameter System.in .同时,您使用参数System.in调用 class 的(所谓的) 构造函数 That means it is going to read from the standard input stream of the program.这意味着它将从程序的标准输入 stream 中读取。

Now when you are calling input.nextInt();现在当你调用input.nextInt(); you execute the method from the object you just created (also documented ).您从刚刚创建的 object 执行方法(也记录在案)。 But as we see, this method returns a integer, so if we want to use that integer, we have to assign the call to a variable like you do:但是正如我们看到的,这个方法返回一个 integer,所以如果我们想使用这个 integer,我们必须像你一样将调用分配给一个变量:

int i = input.nextInt();

Scanner input = new Scanner(System.in); creates a new Scanner instance which points to the input stream passed as argument.创建一个Scanner实例,该实例指向作为参数传递的输入 stream In your case the steam is Standard input stream .在您的情况下,蒸汽是标准输入 stream

So, once your scanner instance is pointing to it, you can scan the stream and get integers , strings and do other stuff.因此,一旦您的扫描仪实例指向它,您就可以扫描stream 并获取integersstrings和做其他事情。

Scanner input = new Scanner(System.in);

Creates a new object of type Scanner from the standard input of the program (in this case probably the console) and从程序的标准输入(在本例中可能是控制台)创建Scanner类型的新 object

int i = input.nextInt()

uses the nextInt Method of that object, which allows you to enter some text and it will be parsed into an integer.使用 object 的nextInt方法,它允许您输入一些文本,它将被解析为 integer。

Scanner s = new Scanner(System.in);

Above statement creates an object of Scanner class which is defined in java.util.scanner package.上面的语句创建了一个 object 的Scanner class ,它在java.util.scanner ZEFE90A8E604A7C86B7AD88 中定义。 Scanner class allows user to take input from console. Scanner class 允许用户从控制台获取输入。

System.in is passed as a parameter in Scanner class. System.inScanner class 中作为参数传递。 It tells the java compiler that system input will be provided through console(keyboard).它告诉 java 编译器系统输入将通过控制台(键盘)提供。

Scanner input = new Scanner(System.in);扫描仪输入=新扫描仪(System.in); it creates a new Scanner instance which points to the input stream passed as argument.它创建一个新的 Scanner 实例,该实例指向作为参数传递的输入 stream。 So here the steam is Standard input stream.所以这里的steam是标准输入stream。

So, once your scanner instance is pointing to it, you can scan the stream and store the value into integers, strings and do other things因此,一旦您的扫描器实例指向它,您就可以扫描 stream 并将值存储为整数、字符串和做其他事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM