我正在学习Java课程,“讲师”正在介绍如何获取用户的输入。我不明白什么是“input.nextLine()”for。
以下是代码:
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 + "'.");
}
}
答案 0 :(得分:3)
用于从输入流中读取下一行。
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
答案 1 :(得分:1)
java.util.Scanner.nextLine()方法使此扫描程序超过当前行并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。该位置设置为下一行的开头。
答案 2 :(得分:0)
它返回下一行,如有必要则等待该行变为可用(即用户键入内容并按下回车键)。