簡體   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