阶段测试练习

 1 import java.util.Scanner;
 2 /**
 3  * @author Administrator
 4  *    从控制台输入字符串,将之全部替换成*号输出
 5  */
 6 public class test1 {
 7     static Scanner input = new Scanner(System.in);
 8     public static void main(String[] args) {
 9         System.out.println("请输入英文字符:");
10         String str = input.next();
11         StringBuffer sb = new StringBuffer(str);
12         for(int i = 0;i<sb.length();i++) {
13             sb.replace(i, i+1, "*");
14         }
15         System.out.println("替换后的字符串为:
"+sb);
16     }
17 }
1、从控制台输入字符串,将之全部替换成*号输出

 1 import java.util.Scanner;
 2 
 3 /**
 4  * @author Administrator
 5  *    截取一段字符串中的数字
 6  */
 7 public class Test2 {
 8     static Scanner input = new Scanner(System.in);
 9     public static void main(String[] args) {
10         System.out.println("请输入字符串:");
11         String str = input.next();
12         StringBuffer num = new StringBuffer();
13         for(int i = 0;i<str.length();i++) {
14             if(str.charAt(i)>=48 && str.charAt(i)<=57) {
15                 num.append(str.charAt(i));
16             }
17         }
18         System.out.println("您的字符串包含以下数字:
"+num.toString());
19     }
20 }
2、截取一段字符串中的数字

 1 import java.util.Scanner;
 2 
 3 /**
 4  * @author Administrator
 5  *    实现简易字符串压缩算法:一个长度最大为128的字符串,由字母a-z或者A-Z组成,
 6  *    将其中连续出现2次以上(含2次)的字母转换为字母和出现次数,
 7  */
 8 public class Test3 {
 9     static Scanner input = new Scanner(System.in);
10     public static void main(String[] args) {
11         System.out.println("请输入一串出英文:");
12         String str = input.next()+" ";
13         StringBuffer sb = new StringBuffer();
14         int num = 1;
15         int i = 0;
16         while(i<str.length()-1) {
17             if(str.charAt(i) == str.charAt(i+1)) {
18                 num++;
19                 i++;
20             }else if(num>1){
21                 sb.append(str.charAt(i));
22                 sb.append(num);
23                 num=1;
24                 i++;
25             }else {
26                 sb.append(str.charAt(i));
27                 i++;
28             }
29         }
30         System.out.println(sb);
31     }
32 }
3、简易字符串压缩算法

 1 import java.io.BufferedReader;
 2 import java.io.BufferedWriter;
 3 import java.io.File;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileReader;
 6 import java.io.FileWriter;
 7 import java.io.IOException;
 8 import java.util.Scanner;
 9 
10 /**
11  * @author Administrator
12  *    文件创建及写入与复制
13  */
14 public class Test4 {
15     static Scanner input = new Scanner(System.in);
16     public static void main(String[] args) {
17         String src = "c:/javabigdata/a.txt";
18         String dest = "c:/javabigdata/b.txt";
19         createFile(src);
20         System.out.println("请输入需要保存的内容:");
21         String s = input.next();
22         Out(src,s);
23         
24         copy(src,dest);
25     }
26     //新建目录及文件
27     public static void createFile(String str) {
28         File file = new File(str);
29         try {
30             file.createNewFile();
31         } catch (IOException e) {
32             // TODO Auto-generated catch block
33             e.printStackTrace();
34         }
35     }
36     
37     //写入数据
38     public static void Out(String str,String s) {
39         BufferedWriter bw = null;
40         FileWriter fw = null;
41         try {
42             fw = new FileWriter(str);
43             bw = new BufferedWriter(fw);
44             bw.write(s);
45         } catch (IOException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         }finally {
49             try {
50                 bw.flush();
51                 bw.close();
52             } catch (IOException e) {
53                 // TODO Auto-generated catch block
54                 e.printStackTrace();
55             }
56         }
57     }
58     
59     //复制文件
60     public static void copy(String src,String dest) {
61         BufferedReader in = null;
62         BufferedWriter out = null;
63         try {
64             in = new BufferedReader(new FileReader(src));
65             out = new BufferedWriter(new FileWriter(dest));
66             
67             String line = null;
68             while ( (line = in.readLine()) != null ) {
69                 out.write(line);
70                 out.newLine();
71             }
72             out.flush();
73         } catch (FileNotFoundException e) {
74             // TODO Auto-generated catch block
75             e.printStackTrace();
76         } catch (IOException e) {
77             // TODO Auto-generated catch block
78             e.printStackTrace();
79         }finally {
80             try {
81                 out.close();
82                 in.close();
83             } catch (IOException e) {
84                 // TODO Auto-generated catch block
85                 e.printStackTrace();
86             }
87             
88         }
89     }
90 }
4、文件创建及写入与复制
 1 import java.text.SimpleDateFormat;
 2 import java.util.Date;
 3 
 4 /**
 5  * @author Administrator
 6  *    在控制台不断按时分秒打印的电子表
 7  */
 8 public class Test5 {
 9     public static void main(String[] args) {
10         SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
11         while(true) {
12             String time = format.format(new Date());
13             System.out.println(time);
14             try {
15                 Thread.sleep(1000);
16             } catch (InterruptedException e) {
17                 // TODO Auto-generated catch block
18                 e.printStackTrace();
19             }
20         }
21     }
22 }
5、在控制台不断按时分秒打印的电子表

 1 /**
 2  * @author Administrator
 3  *    两个线程模拟两个人【一个实现runnable接口,一个继承thread类】,分别叫jack和rose,模拟两人在对话1000次
 4  */
 5 public class Test6Dome {
 6     public static void main(String[] args) {
 7         Test6Syn syn = new Test6Syn();
 8         
 9         Test6Thread num = new Test6Thread(syn);
10         Test6Runnable num1 = new Test6Runnable(syn);
11         
12         Thread thread = new Thread(num);
13         Thread thread1 = new Thread(num1);
14         
15         thread.start();
16         thread1.start();
17     }
18 }
19 
20 
21 实现Runnable
22 public class Test6Runnable implements Runnable{
23     private Test6Syn syn;
24     
25     public Test6Runnable(Test6Syn syn) {
26         super();
27         this.syn = syn;
28     }
29     
30     public void run() {
31         while(true) {
32             for(int i = 0;i<500;i++) {
33                 syn.num1();
34             }
35             break;
36         }
37     }
38 
39 }
40 
41 继承Thread
42 public class Test6Thread extends Thread{
43 private Test6Syn syn;
44     
45     public Test6Thread(Test6Syn syn) {
46         super();
47         this.syn = syn;
48     }
49     
50     public void run() {
51         while(true) {
52             for(int i = 0;i<500;i++) {
53                 syn.num();
54             }
55             break;
56         }
57     }
58 }
59 
60 唤醒机制
61 public class Test6Syn {
62     private boolean flag = false;
63     public synchronized void num() {
64         if(flag) {
65             try {
66                 wait();
67             } catch (InterruptedException e) {
68                 // TODO Auto-generated catch block
69                 e.printStackTrace();
70             }
71         }
72         System.out.println("jack说:我是查水表的!");
73         flag = true;
74         this.notify();
75     }
76     
77     public synchronized void num1() {
78         if(!flag) {
79             try {
80                 wait();
81             } catch (InterruptedException e) {
82                 // TODO Auto-generated catch block
83                 e.printStackTrace();
84             }
85         }
86         System.out.println("rose说:你是谁啊?");
87         flag = false;
88         this.notify();
89     }
90 }
线程题

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test7Dome {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        
        List<Penguin> list = new ArrayList<>();
        list.add(new Penguin("欧欧", "Q仔"));
        list.add(new Penguin("亚亚", "Q妹"));
        list.add(new Penguin("菲菲", "Q妹"));
        list.add(new Penguin("美美", "Q妹"));
        
        System.out.println("共计有" + list.size() + "只企鹅。
分别是:");
        for (Penguin p : list) {
            System.out.println(p.getName() + "	" + p.getSex());
        }
        
        System.out.println("请输入要删除的企鹅名字:");
        String name = input.next();
        int dex =-1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name)) {
                list.remove(i);
                dex = i;
            }
        }
        
        if(dex!=-1) {
            System.out.println("删除成功!");
        }else {
            System.out.println("不存在!");
        }
        
        System.out.println("删除之后还有" + list.size() + "只企鹅
" +"分别是:");
        for (Penguin p : list) {
            System.out.println(p.getName() + "	" + p.getSex());
        }
        
        System.out.println("请输入要查找的企鹅名字:");
        String name1 = input.next();
        int dex1 =-1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getName().equals(name1)) {
                dex1 = i;
            }
        }
        
        if(dex1!=-1) {
            System.out.println("集合中包含" + name1 + "的信息");
        }else {
            System.out.println("集合中不包含" + name1 + "的信息");
        }
    }
}
7、集合题

 1 import java.util.Scanner;
 2 
 3 /**
 4  * @author Administrator
 5  *    会员注册
 6  */
 7 public class Test8 {
 8     static Scanner input = new Scanner(System.in);
 9     static boolean flag = false;
10     public static void main(String[] args) {
11         System.out.println("***欢迎进入注册系统***");
12         while(!flag) {
13             System.out.println("请输入用户名:");
14             String name = input.next();
15             System.out.println("请输入密码:");
16             String psw = input.next();
17             System.out.println("请再次输入密码:");
18             String psw1 = input.next();
19             flag = isOk(name,psw,psw1);
20         }
21         
22     }
23     
24     public static boolean isOk(String name,String psw,String psw1) {
25         if(name.length()<3||psw.length()<6) {
26             flag = false;
27             System.out.println("用户名长度不嫩小于3,密码长度不能小于6!");
28         }else if(!psw.equals(psw1)) {
29             flag = false;
30             System.out.println("两次输入的密码不相同!!");
31         }else {
32             flag = true;
33             System.out.println("注册成功!");
34         }
35         return flag;
36         
37     }
38 }
8、会员注册

 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStream;
 4 import java.io.InputStreamReader;
 5 import java.io.OutputStream;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 /**
10  * @author Administrator
11  *    服务器端
12  */
13 public class Test9Sever {
14     public static void main(String[] args) {
15         try {
16             ServerSocket serversocket = new ServerSocket(8888);
17             Socket socket = serversocket.accept();
18             InputStream is = socket.getInputStream();
19             BufferedReader br = new BufferedReader(new InputStreamReader(is));
20             String info;
21             while((info = br.readLine())!=null) {
22                 System.out.println("客户端说:"+info);
23             }
24             
25             String reply = "欢迎!";
26             OutputStream ot = socket.getOutputStream();
27             ot.write(reply.getBytes());
28             
29             ot.close();
30             br.close();
31             is.close();
32             socket.close();
33             serversocket.close();
34         } catch (IOException e) {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         }
38     }
39 }
40 
41 
42 import java.io.BufferedReader;
43 import java.io.BufferedWriter;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
47 import java.io.OutputStream;
48 import java.net.Socket;
49 
50 /**
51  * @author Administrator
52  *    客户端
53  */
54 public class Test9Client {
55     public static void main(String[] args) {
56         try {
57             Socket socket = new Socket("localhost",8888);
58             String info = "用户名:Dean,密码:123456";
59             OutputStream os = socket.getOutputStream();
60             os.write(info.getBytes());
61             
62             socket.shutdownOutput();
63             InputStream is = socket.getInputStream();
64             BufferedReader br = new BufferedReader(new InputStreamReader(is));
65             String info1;
66             while((info1 = br.readLine())!=null) {
67                 System.out.println("服务器说:"+info1);
68             }
69             
70             br.close();
71             is.close();
72             os.close();
73             socket.close();
74         } catch (IOException e) {
75             // TODO Auto-generated catch block
76             e.printStackTrace();
77         }
78     }
79 }
9、网络编程

 1 import java.io.BufferedInputStream;
 2 import java.io.BufferedOutputStream;
 3 import java.io.FileInputStream;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileOutputStream;
 6 import java.io.IOException;
 7 
 8 /**
 9  * @author Administrator
10  *    图片复制
11  */
12 public class Test10 {
13     public static void main(String[] args) {
14         copy("a.png","b.png");
15     }
16     
17     private static void copy(String src, String dest){
18         BufferedInputStream in = null;
19         BufferedOutputStream out = null;
20         try {
21             in = new BufferedInputStream(new FileInputStream(src));
22             out = new BufferedOutputStream(new FileOutputStream(dest));
23             byte[] buffer = new byte[1024];
24             int len = -1;
25             while ( (len = in.read(buffer)) != -1) {
26                 out.write(buffer, 0, len);
27             }
28         } catch (FileNotFoundException e) {
29             // TODO Auto-generated catch block
30             e.printStackTrace();
31         } catch (IOException e) {
32             // TODO Auto-generated catch block
33             e.printStackTrace();
34         }finally {
35             try {
36                 in.close();
37                 out.close();
38             } catch (IOException e) {
39                 // TODO Auto-generated catch block
40                 e.printStackTrace();
41             }
42         }
43     }
44 }
10、图片复制
原文地址:https://www.cnblogs.com/Dean-0/p/11340896.html