[英]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.