简体   繁体   English

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

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

Here is my method 这是我的方法

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();
}

So if i try to run the method with a given String, it will work at 300 milliseconds, but that is a bit too slow. 因此,如果我尝试使用给定的String运行该方法,它将在300毫秒内工作,但这太慢了。 I want it to run kinda like in the old pokemon games where it was printed fairly fast.. 我希望它像旧的神奇宝贝游戏中那样运行,因为它的打印速度相当快。

If i try to change it to under 300 milliseconds pr char, the output will just stand still until the whole String has been constructed, and then it will print the String.. 如果我尝试将其更改为低于300毫秒,则输出将一直保持静止,直到构造了整个String,然后它将打印String。

Please help as this really annoys me /: 请帮助,因为这确实使我烦恼:

The interval we pass in Thread.sleep(long millis) method is in millis, I ran the above code in my Eclipse with Java 8 , character gets printed as per the interval specified, even if I reduce the interval to 10 it printed the character one by one. 我们在Thread.sleep(long millis)方法中传递的间隔以毫秒为单位,我在Java 8 Eclipse运行了上面的代码,即使指定的间隔减小到10它也会按照指定的间隔打印字符逐一。

What is the interval you tried? 您尝试的间隔是多少?

You can try putting your loop inside the try { }. 您可以尝试将循环放入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.

相关问题 如何使处理程序在微秒而不是毫秒内调用可运行对象? - How can I get my handler to call a runnable in microseconds rather than milliseconds? 扫描仪比打印快? - Scanner faster than print? 在 1 毫秒内删除了 0 个过期的偏移量。 (kafka.coordinator.group.GroupMetadataManager) - Removed 0 expired offsets in 1 milliseconds. (kafka.coordinator.group.GroupMetadataManager) 我无法让我的金字塔 class 打印金字塔 - I cannot get my pyramid class to print the pyramid 有没有比Selenium中的findElement方法更快的方法来获取元素? - Is there a faster method to get elements than the findElement method in Selenium? 我有一个代表毫秒的Longs列表。 如何显示将Longs格式化为MM:SS.LLL的ListView? - I have a list of Longs that represent time in milliseconds. How can I create a ListView that formats the Longs into MM:SS.LLL before displaying them? 如何获得 {300, 200, 400} 的最大值和 {3.5, 5.5, 10.5} 的最大值以在 Java 中打印? - How do I get the max of {300, 200, 400} and the max of {3.5, 5.5, 10.5} to print in Java? SimpleDateFormat无法解析超过4位数的毫秒数 - SimpleDateFormat cannot parse milliseconds with more than 4 digits 无法获取构造函数方法以使用Java打印到控制台 - Cannot get constructor method to print to the console in Java 为什么直接使用print()方法存储数据比将其存储在字符串中然后写入文件要快? - why storing data directly using print() method is faster than storing it in a string and then writing to a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM