我目前正在创建一个 java 程序,它应该再次读取控制台并打印出来。代码如下所示:
import java.io.IOException;
public class printer {
public static void main(String[] args){
int i;
try {
while ((i = System.in.read()) != -1) {
char c = (char)i;
System.out.print(c);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
问题是,如果您在控制台下方键入此文本,您将打印第一行,但由于它"\n"
在“打印”一词之后,程序不会在没有我手动按 Enter 的情况下打印第二行
This is the text I want to print
And now I pressed Enter
当我按下回车键时,得到第二行,结果是:
This is the text I want to print
And now I pressed Enter
这不是它通常的样子。
如果第一行没有自动打印,我会更喜欢。我想按 Enter 并同时获取两条线。可以while ((i = System.in.read()) != -1)
像我一样使用吗?