繁体   English   中英

我无法使printWithDelay方法的打印速度超过300毫秒。

[英]I cannot get my printWithDelay method to print faster than 300 milliseconds.

这是我的方法

public static void printWithDelay(String data, long delay) {
    for (char c : data.toCharArray()) {
        try {
            Thread.sleep(delay);
            System.out.print(c);
        } catch (InterruptedException e) {}
    }
    System.out.println();
}

因此,如果我尝试使用给定的String运行该方法,它将在300毫秒内工作,但这太慢了。 我希望它像旧的神奇宝贝游戏中那样运行,因为它的打印速度相当快。

如果我尝试将其更改为低于300毫秒,则输出将一直保持静止,直到构造了整个String,然后它将打印String。

请帮助,因为这确实使我烦恼:

我们在Thread.sleep(long millis)方法中传递的间隔以毫秒为单位,我在Java 8 Eclipse运行了上面的代码,即使指定的间隔减小到10它也会按照指定的间隔打印字符逐一。

您尝试的间隔是多少?

您可以尝试将循环放入try {}中。

    try {
        for (char c : data.toCharArray()){
             Thread.sleep(delay);
             System.out.print(c);
        }
    } catch (InterruptedException e) {}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM