标签:pfx xtend id3 控制台 and jar scanner 打印 return
测试一
1 package KaoShi; 2 3 import java.util.Scanner; 4 5 public class Demo01 { 6 static Scanner sc = new Scanner(System.in); 7 8 public static void main(String[] args) { 9 10 System.out.println("请输入:"); 11 //接受控制台输入 12 String choose = sc.next(); 13 //把字符串保存, 14 String message = choose; 15 System.out.println("根据法律规定出现违禁字:" + message); 16 //提取需要和谐的内容 17 String regex = "独立|支那人|白痴|滚"; 18 //进行和谐操作 19 message = message.replaceAll(regex, "**"); 20 //显示和谐后的内容: 21 System.out.println(" 根据法律规定出现违禁字已经和谐:\n" +message); 22 } 23 }
测试二
package KaoShi; import java.util.Scanner; public class Demo03 { ///截取一段字符串中的数字 static Scanner input = new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入字符串:"); String str = input.next(); StringBuffer num = new StringBuffer(); for(int i = 0;i<str.length();i++) { if(str.charAt(i)>=48 && str.charAt(i)<=57) { num.append(str.charAt(i)); } } System.out.println("您的字符串包含以下数字:\n"+num.toString()); } }
测试三
实现简易字符串压缩算法:一个长度最大为128的字符串,由字母a-z或者A-Z组成,将其中连续出现2次以上(含2次)的字母转换为字母和出现次数,
* 以达到压缩目的。
* 输入:AAAABBBB
* 输出:A4B4
* 输入:"AAAABBBBCCCCccccddddeeaxckoiiiii"
* 输出:A4B4C4c4d4e2axckoi5
package KaoShi; /** * 实现简易字符串压缩算法:一个长度最大为128的字符串, * 由字母a-z或者A-Z组成,将其中连续出现2次以上(含2次)的字母转换为字母和出现次数, * * */ import java.util.Scanner; public class Demo03 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println("随机输入内容:"); String content = sc.next(); char[] arr = content.toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length;) { sb.append(arr[i]); int j = i; while (j < arr.length && arr[i] == arr[j]) { j++; } if (j - i > 1) { sb.append(j - i); } i = j; } System.out.println(sb.toString()); }
测试四:
package KaoShi; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo04 { public static void main(String[] args) { //创建目录 File f = new File("c:/javabigdata"); //判断目录是否存在 if(f.exists()) { System.out.println("目录已存在"); }else { //若不存在,这创建一个并提示已创建 f.mkdir(); System.out.println("目录已创建"); } //在创建完成的目录下创建一个a.txt FileOutputStream fos = null; //要输出的内容 String content = "I love china,I love beiijng,the Beijing is the capatial of China."; //要输出的内容装换为byte数组 byte b[] = content.getBytes(); try { //明确输出目标 fos = new FileOutputStream("c:/javabigdata/a.txt"); //输出数据 fos.write(b); System.out.println("内容输出成功"); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } try { //关闭输出流 fos.close(); }catch (IOException e) { e.printStackTrace(); } //按题目复制这个刚创建的txt文档 FileInputStream fis = null; try { //明确被复制文件的位置和复制目的地和文件名 fis = new FileInputStream("c:/javabigdata/a.txt"); fos = new FileOutputStream("c:/javabigdata/b.txt"); //根据语法规则设置转移变量numa int num = 0; //根据read()方法,如果读到不为-1的时候,表明读完 while ((num = fis.read()) !=-1){ //写入文件 fos.write(num); } System.out.println("文件复制完毕"); }catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } try { //关闭输入输出流 fos.close(); fis.close(); }catch (IOException e) { e.printStackTrace(); } } }
测试五:
模拟一个在控制台不断按时分秒打印的电子表
package KaoShi; import java.text.SimpleDateFormat; import java.util.Date; //模拟一个在控制台不断按时分秒打印的电子表 public class Demo05 { public static void main(String[] args) { while(true) { //获取当前时间 Date date = new Date(); //创建时间格式 SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); //输出时间 System.out.println(sdf.format(date)); try { //主线程沉睡1000毫秒后再继续循环 Thread.sleep(1000); }catch (InterruptedException e) { e.printStackTrace(); } } } }
测试6:
创建两个线程模拟两个人【一个实现runnable接口,一个继承thread类】,分别叫jack和rose,模拟两人在对话1000次
package Demo06; public class Jack implements Runnable { public void run() { // TODO Auto-generated method stub try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Rose r = new Rose(); Thread t = new Thread(r,"rose"); try { t.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"说:"+" "+"我是查水表"); return; } }
package Demo06; public class Rose extends Thread{ public void run() { // TODO Auto-generated method stub try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"说:"+" "+"你是谁呀?"); return; } }
package Demo06; public class Test { public static void main(String[] args) { Jack j = new Jack(); Rose r = new Rose(); for (int i = 0; i < 1000; i++) { Thread t1 = new Thread(j,"jack"); Thread t2 = new Thread(r,"rose"); try { t2.join(); Thread.sleep(500); t1.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } t1.start(); t2.start(); } } }
测试七:
使用List存储企鹅【penguin】信息
需求说明:把多个企鹅的信息添加到集合中,查看企鹅的数量及所有企鹅的信息,删除集合中部分企鹅的元素,判断集合中是否包含指定企鹅
package Demo07; public class Penguin { private String name; private String strain; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getStrain() { return strain; } public void setStrain(String strain) { this.strain = strain; } public Penguin(String name, String strain) { super(); this.name = name; this.strain = strain; } }
package Demo07; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { Penguin p1 = new Penguin("欧欧", "Q仔"); Penguin p2 = new Penguin("亚亚", "Q妹"); Penguin p3 = new Penguin("菲菲", "Q妹"); Penguin p4 = new Penguin("美美", "Q妹"); // 加入List List<Penguin> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); System.out.println("共计有" + list.size() + "只企鹅。\n分别是:"); for (Penguin penguin : list) { System.out.println(penguin.getName() + "\t" + penguin.getStrain()); } // 删除信息 for (int i = 0; i < list.size(); i++) { if (list.get(i).getName().equals("菲菲")) { list.remove(i); } if (list.get(i).getName().equals("美美")) { list.remove(i); } } System.out.println("\n删除之后还有" + list.size() + "只企鹅。\n分别是:"); for (Penguin penguin : list) { System.out.println(penguin.getName() + "\t" + penguin.getStrain()); } // 判断列表内是否有该内容 String str = "美美"; // boolean boo=false; for (Penguin penguin : list) { if (str.equals(penguin.getName())) { System.out.println("集合包含" + str); break; } else { System.out.println("集合不包含" + str); break; } } } }
测试8:
需求说明:实现会员注册
要求,用户名长度不小于3,密码长度不小于6,注册时两次输入密码必须相同
package KaoShi; import java.util.Scanner; /** 实现注册功能,用户名不能小于3,密码不能小于6,两次密码要一致 */ public class Demo08 { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { System.out.println("***欢迎来到注册系统***"); System.out.println("请输入用户名:"); String account = sc.next(); while (account.length() < 3) { System.out.println("用户名长度不能小于3,请重新输入:"); account = sc.next(); } System.out.println("请输入注册密码:"); String password = sc.next(); for (; password.length() < 6;) { System.out.println("注册密码长度不能小于6,请重新输入!"); password = sc.next(); } System.out.println("请再次输入密码:"); String password2 = sc.next(); if (!password2.equals(password)) { System.out.println("您输入的密码有误,两次密码不一致,请重新输入:"); password2 = sc.next(); } System.out.println("注册成功!感谢您使用注册系统!"); } }
测试9:
测试10:
package KaoShi; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo10 { //复制图片 //1、地址 2、读取 FileInput 再输出FileOut public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("C:/Users/admin/Desktop/1.jpg"); fos = new FileOutputStream("C:/Users/admin/Desktop/2.jpg"); int num = -1; try { while((num=fis.read())!=-1) { fos.write(num); fos.flush(); } System.out.println("复制成功!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { fos.close(); fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
标签:pfx xtend id3 控制台 and jar scanner 打印 return
原文地址:https://www.cnblogs.com/jingkai555/p/11354645.html