если вы видите код ниже от Scanner.class, вы узнаете свой ответ.
/**
* Returns true if this scanner has another token in its input.
* This method may block while waiting for input to scan.
* The scanner does not advance past any input.
*
* @return true if and only if this scanner has another token
* @throws IllegalStateException if this scanner is closed
* @see java.util.Iterator
*/
public boolean hasNext() {
ensureOpen();
saveState();
while (!sourceClosed) {
if (hasTokenInBuffer())
return revertState(true);
readInput();
}
boolean result = hasTokenInBuffer();
return revertState(result);
}
Если вы видите вышеупомянутый код метода sureOpen (), убедитесь, что сканер открыт или закрыт, отправьте сообщение состояние подтверждено, что означает, что до тех пор, пока ваш курсор не получит ввод, он будет проверять информационную форму конструктора сканера с параметром.
Здесь При определении приведенный ниже код.
Scanner out = new Scanner(System.in);
Здесь, создание нового ресурса сканера принимает вход системы в качестве потока. Ниже конструктор с параметром выполняет задачу.
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param source An input stream to be scanned
*/
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN); // Below is the description
}
Здесь, в коде комментария, отмеченном выше, вызов переходит к классу java .io.InputStream, который гарантирует, что чтение берется из консоли до Пользователь вводит данные.
Надеюсь, это поможет вам.