Java Scanner input = new Scanner(System.in) 实际上是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30612811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does Scanner input = new Scanner(System.in) actually mean?
提问by The Man
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()
采纳答案by moffeltje
Alright, let's elaborate with some simplified explanation about the Scanner
class.
好的,让我们详细说明一些关于Scanner
该类的简化解释。
It is a standard Oracle class which you can use by calling the import java.util.Scanner
.
它是一个标准的 Oracle 类,您可以通过调用import java.util.Scanner
.
So let's make a basic example of the 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);
you make a new object of the Scanner
class (so you make a new "Scanner") and you store it in the variable input
. At the same time you are calling the (so called) constructorof the class, with the parameter System.in
. That means it is going to read from the standard input stream of the program.
现在,当您调用时,您Scanner input = new Scanner(System.in);
会创建Scanner
该类的一个新对象(因此您创建了一个新的“扫描仪”)并将其存储在变量中input
。同时,您正在使用参数调用类的(所谓的)构造函数System.in
。这意味着它将从程序的标准输入流中读取。
Now when you are calling input.nextInt();
you execute the method from the object you just created (also documented). 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:
现在,当您调用时,您input.nextInt();
从刚刚创建的对象执行该方法(也记录在案)。但是正如我们看到的,这个方法返回一个整数,所以如果我们想使用这个整数,我们必须像你一样将调用分配给一个变量:
int i = input.nextInt();
回答by TheLostMind
Scanner input = new Scanner(System.in);
creates a newScanner
instance which points to the input streampassed as argument. In your case the steam is Standard input stream.
Scanner input = new Scanner(System.in);
创建一个指向作为参数传递的输入流的新Scanner
实例。在您的情况下,蒸汽是标准输入流。
So, once your scanner instance is pointing to it, you can scanthe stream and get integers
, strings
and do other stuff .
因此,一旦您的扫描仪实例指向它,您就可以扫描流并获取integers
,strings
并执行其他操作。
回答by Tim Kathete Stadler
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
从程序的标准输入(在本例中可能是控制台)创建一个新的类型对象
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.
使用该nextInt
对象的方法,它允许您输入一些文本,它将被解析为一个整数。
回答by DEVENDRA PARIHAR
Scanner s = new Scanner(System.in);
Scanner s = new Scanner(System.in);
Above statement we create an object of a Scanner class which is define in import java.util.scannerpackage. scanner class allows user to take input form console.
上面的语句我们创建了一个 Scanner 类的对象,该对象在import java.util.scanner包中定义。扫描仪类允许用户输入表单控制台。
System.inis passed as parameter in scanner class will tell to java compiler system input will be provided through console(keyboard).
System.in作为参数传递给扫描器类将告诉 java 编译器系统输入将通过控制台(键盘)提供。