当您使用Java扫描程序类接收用户输入时,对于实际发生的情况有何准确的解释?

时间:2022-08-28 14:37:09

I simply need to know if I understand the semantics of this simple Java statement. And I am not the only one. I have Googled this question and lots of people have been asking but everyone gives the same answer or gives regurgitated information from the official documentation. We know how to use it. We want to know how it works. Why.

我只需要知道我是否理解这个简单Java语句的语义。而且我不是唯一的一个。我用Google搜索了这个问题,很多人一直在问,但是每个人都给出了相同的答案,或者从官方文档中提供反刍信息。我们知道如何使用它。我们想知道它是如何工作的。为什么。

I know (and so do the others who've asked this) how to use the scanner class. I know how to accept user input and store that input in a variable. But I feel that I should not go any further till I know for sure I understand WHY it works and HOW it works. As it involves Instantiation, Classes, Objects, etc. which is a huge part of this language. It IS the language.

我知道(其他人问过这个问题)如何使用扫描仪类。我知道如何接受用户输入并将该输入存储在变量中。但是我觉得我不应该继续下去,直到我知道我明白为什么它有效以及如何运作。因为它涉及实例化,类,对象等,这是该语言的重要组成部分。这是语言。

What I would like to do is tell you what I personally think is going on in the code and have you all tell me if that is accurate or not. Ok, here we go.

我想做的是告诉你我个人认为在代码中发生了什么,并且你们都告诉我这是否准确。好的,我们走了。

Scanner keyboard;
keyboard = new Scanner(System.in);
String userInput = keyboard.next();

What is happening here?

这里发生了什么?

We want to receive user input from the keyboard so we decide to use the scanner class to achieve this.

我们希望从键盘接收用户输入,因此我们决定使用scanner类来实现此目的。

1.) The first thing we do is declare our variable. We name it keyboard in the declaration. (Is the variable keyboard "type scanner"? What is the variable type here? Scanner, Object, class, etc. It's certainly not a primitive.)

1.)我们要做的第一件事是声明我们的变量。我们在声明中将其命名为键盘。 (可变键盘是“类型扫描仪”吗?这里的变量类型是什么?扫描仪,对象,类等等。它肯定不是原始的。)

2.) Now use the java keyword and operator **new to instantiate (create) an object based off the Scanner. We now assign this object to the keyboard variable we previously declared.**

2.)现在使用java关键字和operator ** new来实例化(创建)基于Scanner的对象。我们现在将此对象分配给我们之前声明的键盘变量。**

3.) We pass in System.in as the data source which in this case is the keyboard.

3.)我们传入System.in作为数据源,在这种情况下是键盘。

4.) Now we want to receive the user's input from the keyboard so we declare a new variable named userInput. and then invoke the next() method on the newly instantiated keyboard object and assign this as the value to the new variable userInput (is this a call to a constructor? Which part is the call to the constructor?)

4.)现在我们想要从键盘接收用户的输入,因此我们声明了一个名为userInput的新变量。然后在新实例化的键盘对象上调用next()方法,并将其作为值赋给新变量userInput(这是对构造函数的调用吗?哪一部分是对构造函数的调用?)

I've asked a few different questions here and even if this gets downvoted to all hell, please in the very least help me with some answers. I will be VERY grateful. You have no idea. Thank you all.

我在这里问了几个不同的问题,即使这个问题一切都没问题,请至少帮助我一些答案。我将非常感激。你不知道。谢谢你们。

1 个解决方案

#1


1  

Look at the comments. Hopefully they are clear enough.

看看评论。希望他们足够清楚。

// declare a variable of type `Scanner`
Scanner keyboard; 

// call constructor of class `Scanner` and pass `Systen.in` as a parameter to create a new instance 
// and assign its reference to `keyboard`
keyboard = new Scanner(System.in);  

// declare a variable of type `String` and 
// assign to it the return value of `next()` method called on `keyboard` object
String userInput = keyboard.next(); 

#1


1  

Look at the comments. Hopefully they are clear enough.

看看评论。希望他们足够清楚。

// declare a variable of type `Scanner`
Scanner keyboard; 

// call constructor of class `Scanner` and pass `Systen.in` as a parameter to create a new instance 
// and assign its reference to `keyboard`
keyboard = new Scanner(System.in);  

// declare a variable of type `String` and 
// assign to it the return value of `next()` method called on `keyboard` object
String userInput = keyboard.next();