Java:屏幕打印延迟

时间:2018-08-30 19:39:45

标签: java java-threads system.out

因此,我有一个功能可以像在屏幕上键入文本一样打印出文本。

我尝试使用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();
}

1 个答案:

答案 0 :(得分:1)

输出已缓冲。要刷新缓冲区,您需要调用flush或在字符串中包含\n

相关问题