gpt4 book ai didi

Java:打印到屏幕被延迟

转载 作者:行者123 更新时间:2023-12-02 10:51:56 26 4
gpt4 key购买 nike

所以我有一个函数应该打印出文本,就像在屏幕上键入文本一样。

我尝试使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com