I want to make text appear in the following way:
H
wait 0.1 seconds
He
wait 0.1 seconds
Hel
wait 0.1 seconds
Hell
wait 0.1 seconds
Hello
But I'm not sure how to do it. Any help would be appreciated.
EDIT: I'm hoping that I will be able to do it in a way that doesn't require me to make a System.out.print(); for each letter.
EDIT 3: Here's my code. EDIT 4: No more problems, it worked perfectly, thanks.
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class GameBattle
{
public static void main(String[] args) throws Exception {
printWithDelays("HELLO", TimeUnit.MILLISECONDS, 100);
}
public static void printWithDelays(String data, TimeUnit unit, long delay)
throws InterruptedException {
for (char ch:data.toCharArray()) {
System.out.print(ch);
unit.sleep(delay);
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…