java 中如何实现:从键盘读入若干行,如果为空则停止输入

2024-12-03 21:54:13
推荐回答(2个)
回答1:

键盘输入若干行,输入空格按回合结束
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("输入空格结束按回车结束");
StringBuffer inputStr = new StringBuffer();
System.out.print("请输入: ");
do {
String line = reader.readLine();
System.out.println(line);
if (line.equals(" ")) {
break;
}
inputStr.append(line+"\n");
} while (true);

System.out.println(inputStr.toString());
}

回答2:

BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));