使用 Scanner 读取系统输入

Scanner scanner = new Scanner(System.in); //Scanner obj to read System input
String inputTaken = new String();
while (true) {
    String input = scanner.nextLine(); // reading one line of input
    if (input.matches("\\s+"))         // if it matches spaces/tabs, stop reading
        break;
    inputTaken += input + " ";
}
System.out.println(inputTaken);

初始化扫描仪对象以从键盘读取输入。因此,对于来自 keyboar 的以下输入,它将产生输出为 Reading from keyboard

Reading
from
keyboard
  //space