java - 使用单个扫描仪对象扫描多行

标签 java input output java.util.scanner

我是 Java 的新手,所以如果这对您来说听起来绝对愚蠢,请不要打分

好的,我如何使用单个扫描仪对象输入这个

5

hello how do you do

welcome to my world

6 7

给那些建议的人

scannerobj.nextInt->nextLine->nextLine->nextInt->nextInt,,,

检查一下,它不起作用!!!

谢谢

最佳答案

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

    System.out.printf("Please specify how many lines you want to enter: ");        
    String[] input = new String[in.nextInt()];
    in.nextLine(); //consuming the <enter> from input above

    for (int i = 0; i < input.length; i++) {
        input[i] = in.nextLine();
    }

    System.out.printf("\nYour input:\n");
    for (String s : input) {
        System.out.println(s);
    }
}

示例执行:

Please specify how many lines you want to enter: 3
Line1
Line2
Line3

Your input:
Line1
Line2
Line3

关于java - 使用单个扫描仪对象扫描多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22929819/

相关文章:

go - 如何在golang中使用空格距离进行输入

php - PHP foreach后返回不同的输入

python - 重新排序文本文件 - Python

c - .o 与 C 中的 .out

java - 查找 A^5 + B^5 + C^5 所有可能值的算法?

java - Apache POI - 基于 2 个日期参数导出到 Excel

java - Spring 启动: execute a job at a particular Date entered by the user

Haskell Gloss - 在按下每帧键时执行一些操作

java - 更改MapReduce(K,V)输出格式

java - 如何将 JSP 页面解析为 XML 文件?