要看起来一个字一个字的输出就需要停顿时间,这样才能看得到这种效果,停顿的话又需要新建线程。
代码如下:
public class AutomaticTyping {
public static void main(String[] args) {
ATy aty = new ATy();// 创建一个对象
Thread t = new Thread(aty);// 创建一个线程
t.start();// 线程开始
}
}
class ATy implements Runnable {
String text = "秋名山上行人稀,常有车手较高低。\n如今车道依旧在,不见当年老司机。";
public void run() {
for (int i = 0; i < text.length(); i++) {
System.out.print(text.charAt(i));
try {
Thread.sleep(200);//停顿0.2秒
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
方法不唯一。
