Java input.nextLine() 的含义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24940873/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:51:59  来源:igfitidea点击:

Meaning of input.nextLine()

javainputuser-input

提问by user3658616

I am taking a course on Java and the "instructor" is introducing how to get the user's input. I don't understand what is the "input.nextLine()" for.

我正在修一门关于 Java 的课程,“讲师”正在介绍如何获取用户的输入。我不明白什么是“input.nextLine()”。

Here's the code:

这是代码:

import java.util.Scanner;

public class Application {
    public static void main(String[] args) {

        // Create Scanner object
        Scanner input = new Scanner(System.in);

        // Output the prompt
        System.out.println("Type in something: ");

        // Wait for the user to enter a line of text
        String line = input.nextLine();

        // Tell them what they entered
        System.out.println("You just typed " + "'" + line + "'.");
    }
}

采纳答案by TheLostMind

It is for reading the next line from the input stream.

它用于从输入流中读取下一行。

Scanner input = new Scanner(System.in); 
// create a new reference and refer to the input stream.
String line = input.nextLine(); 
// read the next line from the stream (entered from the keyboard) and store it in a String variable named line

回答by Kayaman

It returns the next line, waiting if necessary for the line to become available (i.e. the user types out something and presses enter).

它返回下一行,必要时等待该行变为可用(即用户输入某些内容并按回车键)。

回答by Sonal Kumar

The java.util.Scanner.nextLine() method advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

java.util.Scanner.nextLine() 方法将此扫描器前进到当前行并返回被跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。