1

私はいくつかのテキスト文字を文字ごとに少し遅れて印刷しようとしていますが、問題はそれが待ってから待ってから文全体を印刷することです。文字ごとに文字列を出力し、終了したらその文字列を出力するようなものです。

public static void printWithDelay(String data, TimeUnit unit, long delay) 
  throws InterruptedException {
    for (char ch : data.toCharArray()) {
        System.out.print(ch);
        unit.sleep(delay);
    }
}

助けてください (:

4

3 に答える 3

1

なぜあなたは使わないのですThread.sleep()か?

import java.lang.*;

public class PrintWithDelayExample {
    public static void main(String[]args) {
        printWithDelay("Hello! World", 500);
    }

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

スリープによる実行の一時停止を参照してください

そして、スレッドスリープを適切に使用する方法

于 2015-10-08T15:38:51.710 に答える