码迷,mamicode.com
首页 > 编程语言 > 详细

java模拟自动打字效果代码

时间:2015-01-30 19:05:04      阅读:318      评论:0      收藏:0      [点我收藏+]

标签:

思路:

1:把字符串转成为字符串数组,用for依次打印数组下标,用线程的sleep来延时。

2:用String里面的CharAt()方法,再配合for循环来打印指定索引处的字符,用sleep来延时。

 

 1 public class AutomaticTyping {
 2 
 3     public static void main(String[] args){
 4     ATy aty = new ATy();//创建一个对象
 5     Thread t = new Thread(aty);//创建一个线程
 6     t.start();//线程开始
 7     
 8     }
 9 
10 }
11 class ATy implements Runnable{
12     String wenben ="少壮不努力,老大敲代码\n-----By H4ckSo1di3r";
13     
14     public void run(){    
15         for(int i=0; i<wenben.length();i++){
16             System.out.print(wenben.charAt(i));
17             try {
18                 Thread.sleep(200);
19             } catch (InterruptedException e) {
20                 e.printStackTrace();
21             }
22         }        
23 //         另一种写法 把字符串先转换成字符串数组 再把依次打印
24 //         char [] a =wenben.toCharArray();
25 //         for(int i = 0;i<a.length;i++){
26 //         System.out.print(a[i]);
27 //         try {
28 //                Thread.sleep(200);
29 //            } catch (InterruptedException e) {
30 //                // TODO Auto-generated catch block
31 //                e.printStackTrace();
32 //            }
33 //        }
34 }
35     }

 

java模拟自动打字效果代码

标签:

原文地址:http://www.cnblogs.com/H4ckSo1di3r/p/4262816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!