小弟使用线程编制一程序,使得字符串"文字的打印效果"每隔1秒中输出一个字符。
运行结果:
起初:文
1秒后:文字
2秒后:文字的
...
7秒后:文字的打印效果以下是我写的程序,不能实现每秒钟输出一个字符(从起初~7秒:都是输出一个字符),请大哥们修改哈
public class Alone14_2 extends Thread{
String a="文字的打印效果";
public void run(){
System.out.println("起初:文");
for(int i=1;i<a.length();i++)
try{
sleep(1000);
System.out.println(i+"秒后:"+a.charAt(i));
}catch(InterruptedException ie){}
}
public static void main(String[] args){
Alone14_2 t=new Alone14_2();
t.start();
}
}
运行结果:
起初:文
1秒后:文字
2秒后:文字的
...
7秒后:文字的打印效果以下是我写的程序,不能实现每秒钟输出一个字符(从起初~7秒:都是输出一个字符),请大哥们修改哈
public class Alone14_2 extends Thread{
String a="文字的打印效果";
public void run(){
System.out.println("起初:文");
for(int i=1;i<a.length();i++)
try{
sleep(1000);
System.out.println(i+"秒后:"+a.charAt(i));
}catch(InterruptedException ie){}
}
public static void main(String[] args){
Alone14_2 t=new Alone14_2();
t.start();
}
}
改为
System.out.println(i+"秒后:"+a.substring
(0,i+1));