所以我有一个函数应该打印出文本,就像在屏幕上键入文本一样。
我尝试使用 System.nanoTime() 设置并获取打印到屏幕所需的时间,并将其从 Thread.sleep 中删除,但仍然不顺利。
我希望打印每个字母并花费大致相同的时间,而不是像现在那样在字母之间来回拉动。
public static void slow_print(String to_print) {
long time_between = 200;
for (int i = 0; i < to_print.length(); i++) {
System.out.print(to_print.charAt(i));
try {
Thread.sleep(time_between);
}
catch(InterruptedException event) {
System.out.println("Not supposed to happen");
}
}
System.out.println();
}
最佳答案
输出已缓冲。要刷新缓冲区,您需要调用 flush
或将 \n
包含到字符串中。
关于Java:打印到屏幕被延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52104272/