如何在没有打印语句的情况下扫描多行

how to scan multiple lines without print statement

'''
    Scanner sc = new Scanner(System.in); 
    ArrayList<Integer> arrayIntegers = new ArrayList<Integer>();
    System.out.print("#ofints: ");          
    String[] arrayStrings = new String [sc.nextInt()];  
    sc.nextLine(); 

    for (int i = 0; i < arrayStrings.length; i++)   
    {  
        arrayStrings[i] = sc.nextLine();  
    }  
'''

-如何使用 hasNext() 方法完成此操作(如果输入 space 则中断),因此不需要用户提示语句。 例如:

1

2

3

而不是:

#ofints: 3

1

2

3

hasNext() 方法检查 Scanner 的输入中是否有另一个标记。扫描器使用分隔符模式将其输入分解为标记,默认情况下匹配空格。

检查这个(停止输入“退出”):

Scanner sc = new Scanner(System.in);
String inputStr;

while (sc.hasNext()) {
    inputStr = sc.next();
    if (inputStr.equals("exit")) break;
    System.out.println(inputStr);
}

P.S。 hasNextLine() 方法检查 Scanner 对象的输入中是否有另一行,无论该行是否为空。