欢迎访问宙启技术站
智能推送

nextLine函数从控制台读取一行文本输入

发布时间:2023-08-13 12:58:11

下面是一个使用Java编写的示例代码,演示如何使用nextLine函数从控制台读取一行文本输入:

import java.util.Scanner;

public class ReadInputText {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入文本:");
        String text = scanner.nextLine();

        System.out.println("你输入的文本是:");
        System.out.println(text);
    }
}

请注意,该代码使用了Scanner类,该类提供了一系列方法来读取不同类型的输入。在上述示例中,我们使用了nextLine方法来读取一行文本输入。

当你运行上述代码时,它会提示你输入文本。你可以输入多达1000个字符的文本,按下Enter键确认输入。然后,该代码将输出你输入的文本。