常用工具类练习

1. 请根据控制台输入的特定日期格式拆分日期,如:请输入一个日期(格式如:**月**日****年),经过处理得到:****年**月**日
  1. import java.util.Scanner;
  2.  
  3. public class One {
  4. public static void main(String[] args){
  5. Scanner sc=new Scanner(System.in);
  6. String a=sc.next();
  7. int a1=a.indexOf("年");
  8. int a2=a.indexOf("月");
  9. int a3=a.indexOf("日");
  10. String a4=a.substring(a3+1,a1+1);
  11. String a5=a.substring(0,a3+1);
  12. System.out.println(a4+a5);
  13. }
  14. }

运行结果:

3月3日2019年

2019年3月3日

2. 给出一个随机字符串,判断有多少字母?多少数字?
  1. import java.util.Scanner;
  2.  
  3. public class Two {
  4.  
  5. public static void main(String[] args){
  6.  
  7. Scanner sc=new Scanner(System.in);
  8.  
  9. String b=sc.next();
  10.  
  11. char[] b1=b.toCharArray();
  12.  
  13. int x=0,y=0;
  14.  
  15. for(char b2:b1){
  16.  
  17. if(Character.isLetter(b2)){
  18.  
  19. x++;
  20.  
  21. }else if(Character.isDigit(b2)){
  22.  
  23. y++;
  24.  
  25. }
  26.  
  27. }
  28.  
  29. System.out.println("字母的个数:"+x+"数字的个数:"+y);
  30.  
  31. }
  32.  
  33. }

运行结果:

aaa45a

字母的个数:4数字的个数:2

3. 以下是一段歌词,请从这段歌词中统计出朋友出现的次数,"这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。"

方法一:

  1. import com.sun.xml.internal.ws.util.StringUtils;
  2.  
  3. public class Three {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. String c = new String("这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我");
  8.  
  9. int num=0;
  10.  
  11. for(int i=0;i<c.length()-1;i++){
  12.  
  13. String cc=c.substring(i,i+2);
  14.  
  15. if(cc.equals("朋友")){
  16.  
  17. num++;
  18.  
  19. }
  20.  
  21. }
  22.  
  23. System.out.println(num);
  24.  
  25. }
  26. }

运行结果:

3

方法二:

  1. public class Work_3 {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. String s = "这些年一个人,风也过,雨也走,有过泪,有过错,"+
  6.  
  7. " 还记得坚持甚么,真爱过才会懂,会寂寞会回首," +
  8.  
  9. "终有梦终有你在心中。朋友一生一起走,那些日子不再有," +
  10.  
  11. "一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂," +
  12.  
  13. "还有伤,还有痛,还要走,还有我。";
  14.  
  15. int num=0;
  16.  
  17. getNumber(s,num);
  18.  
  19. }
  20.  
  21. public static void getNumber(String s,int num){
  22.  
  23. int n = s.indexOf("朋友");
  24.  
  25. if(n>=0){
  26.  
  27. num++;
  28.  
  29. s=s.substring(n+2);
  30.  
  31. getNumber(s,num);
  32.  
  33. }else{
  34.  
  35. System.out.println(num);
  36.  
  37. }
  38.  
  39. }
  40.  
  41. }

运行结果:

3

方法三:

  1. public class Work_3_2 {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. String s = "这些年一个人,风也过,雨也走,有过泪,有过错,"+
  6.  
  7. " 还记得坚持甚么,真爱过才会懂,会寂寞会回首," +
  8.  
  9. "终有梦终有你在心中。朋友一生一起走,那些日子不再有," +
  10.  
  11. "一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂," +
  12.  
  13. "还有伤,还有痛,还要走,还有我。";
  14.  
  15. String[] s1 = s.split("朋友");
  16.  
  17. System.out.println(s1.length-1));
  18. }
  19.  
  20. }

运行结果:

3

方法四:

  1. public class Work_3_4 {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. String s = "这些年一个人,风也过,雨也走,有过泪,有过错,"+
  6.  
  7. " 还记得坚持甚么,真爱过才会懂,会寂寞会回首," +
  8.  
  9. "终有梦终有你在心中。朋友一生一起走,那些日子不再有," +
  10.  
  11. "一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂," +
  12.  
  13. "还有伤,还有痛,还要走,还有我。";
  14.  
  15. String key ="朋友";
  16.  
  17. int start = s.length();
  18.  
  19. String s1 = s.replace(key,"");
  20.  
  21. int end = s1.length();
  22.  
  23. int count = (start-end)/key.length();
  24.  
  25. System.out.println(count);
  26. }
  27.  
  28. }

运行结果:

3

4. 编写敏感词过滤程序 ,说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤
  1. public class Four {
  2.  
  3. public static void main(String[] args){
  4.  
  5. String[] d={"枪","爆炸","恐怖"};
  6.  
  7. // Scanner sc=new Scanner(System.in);
  8.  
  9. // String d1=sc.next();
  10.  
  11. String d1=new String("不要拿枪,很恐怖,会爆炸");
  12.  
  13. for(String d2:d){
  14.  
  15. d1=d1.replace(d2,"**");
  16.  
  17. }
  18.  
  19. System.out.println(d1);
  20.  
  21. }
  22.  
  23. }

运行结果:

不要拿**,很**,会**

5. 根据输入的年份、产品类型和随机数产生固定资产编号

即:固定资产编号=年份+0+产品类型+3位随机数

程序运行流程:请输入年份:

……

请选择产品类型(1. 台式机 2. 笔记本 3. 其他):

……

生成3位随机数

最后显示固定资产编号

提示:3位随机数按如下方法产生:

(int)(Math.random()*1000);

  1. public class Five {
  2.  
  3. public static void main(String[] args){
  4.  
  5. System.out.println("请输入年份:");
  6.  
  7. Scanner s =new Scanner(System.in);
  8.  
  9. String e=s.next();
  10.  
  11. System.out.println("请选择产品类型:1.台式机,2.笔记本,3.其他");
  12.  
  13. Scanner s1 =new Scanner(System.in);
  14.  
  15. String e1=s1.next();
  16.  
  17. int e2=(int)(Math.random()*1000);
  18.  
  19. String e3=Integer.toString(e2);
  20.  
  21. String e4=e.concat(e1).concat(e3);
  22.  
  23. System.out.println("固定资产编号为:"+e4);
  24.  
  25. }
  26.  
  27. }
  1. 运行结果:

请输入年份:

2019

请选择产品类型:1.台式机,2.笔记本,3.其他

2

固定资产编号为:20192527

6. 编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。
  1. public class Eight {
  2.  
  3. public static void main(String[] args){
  4.  
  5. String h=new String("aaSdEcWq");
  6.  
  7. char[] h1=h.toCharArray();
  8.  
  9. StringBuffer h3=new StringBuffer();
  10.  
  11. StringBuffer h4=new StringBuffer();
  12.  
  13. for(char h2:h1){
  14.  
  15. if(Character.isUpperCase(h2)){
  16.  
  17. h3.append(h2);
  18.  
  19. h3.append(",");
  20.  
  21. }else if(Character.isLowerCase(h2)){
  22.  
  23. h4.append(h2);
  24.  
  25. h4.append(",");
  26.  
  27. }
  28.  
  29. }
  30.  
  31. System.out.println(h3);
  32.  
  33. System.out.println(h4);
  34.  
  35. }
  36.  
  37. }

运行结果:

S,E,W,

a,a,d,c,q,

7. 计算并输出21世纪的闰年,计算程序的执行时间
  1. public class Seven {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. long start = System.currentTimeMillis();
  6.  
  7. for (int i=2000;i<2100;i++){
  8.  
  9. if(i%4==0){
  10.  
  11. System.out.println("21世纪所有的闰年:"+i);
  12.  
  13. }
  14.  
  15. }
  16.  
  17. long end = System.currentTimeMillis();
  18.  
  19. long run = end-start;
  20.  
  21. System.out.println("程序运行时间为:"+run+"毫秒");
  22.  
  23. }
  24. }

运行结果:

21世纪所有的闰年:2000

21世纪所有的闰年:2004

21世纪所有的闰年:2008

21世纪所有的闰年:2012

21世纪所有的闰年:2016

21世纪所有的闰年:2020

21世纪所有的闰年:2024

21世纪所有的闰年:2028

21世纪所有的闰年:2032

21世纪所有的闰年:2036

21世纪所有的闰年:2040

21世纪所有的闰年:2044

21世纪所有的闰年:2048

21世纪所有的闰年:2052

21世纪所有的闰年:2056

21世纪所有的闰年:2060

21世纪所有的闰年:2064

21世纪所有的闰年:2068

21世纪所有的闰年:2072

21世纪所有的闰年:2076

21世纪所有的闰年:2080

21世纪所有的闰年:2084

21世纪所有的闰年:2088

21世纪所有的闰年:2092

21世纪所有的闰年:2096

程序运行时间为:1毫秒

Java项目案例之---常用工具类练习的更多相关文章

  1. 自己项目中PHP常用工具类大全分享

    <?php /** * 助手类 * @author www.shouce.ren * */ class Helper { /** * 判断当前服务器系统 * @return string */ ...

  2. (转)JAVA 十六个常用工具类

    一. org.apache.commons.io.IOUtils closeQuietly 关闭一个IO流.socket.或者selector且不抛出异常.通常放在finally块 toString ...

  3. 项目经验分享——Java常用工具类集合 转

    http://blog.csdn.net/xyw591238/article/details/51678525 写在前面     本文涉及的工具类部分是自己编写,另一部分是在项目里收集的.工具类涉及数 ...

  4. JAVA常用工具类汇总

    一.功能方法目录清单: 1.getString(String sSource)的功能是判断参数是否为空,为空返回"",否则返回其值: 2.getString(int iSource ...

  5. JavaEE-实验一 Java常用工具类编程

    该博客仅专为我的小伙伴提供参考而附加,没空加上代码具体解析,望各位谅解 1.  使用类String类的分割split 将字符串  “Solutions to selected exercises ca ...

  6. 简单了解Spring中常用工具类_java - JAVA

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...

  7. vue项目工具文件utils.js javascript常用工具类,javascript常用工具类,util.js

    vue项目工具文件utils.js :https://blog.csdn.net/Ajaxguan/article/details/79924249 javascript常用工具类,util.js : ...

  8. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  9. Java常用工具类之删除文件

    package com.wazn.learn.util; import java.io.File; /** * 删除文件工具类 * @author yangzhenyu * */ public cla ...

随机推荐

  1. Windows 上静态编译 Libevent 2.0.10 并实现一个简单 HTTP 服务器(无数截图)

    [文章作者:张宴 本文版本:v1.0 最后修改:2011.03.30 转载请注明原文链接:http://blog.s135.com/libevent_windows/] 本文介绍了如何在 Window ...

  2. Win8 Metro(C#)数字图像处理--2.71Sigma平滑滤波器

    原文:Win8 Metro(C#)数字图像处理--2.71Sigma平滑滤波器  [算法说明]   Sigma平滑滤波器是构造一个模板,比如3*3大小的模板,计算这个模板对应的像素的标准差d,然后 ...

  3. 【转】python Counter模块

    >>> c = Counter() # 创建一个新的空counter >>> c = Counter('abcasdf') # 一个迭代对象生成的counter & ...

  4. UWP中的消息提示框(二)

    在UWP中的消息提示框(一)中介绍了一些常见的需要用户主动去干涉的一些消息提示框,接下来打算聊聊不需要用户主动去干涉的一些消息提示框.效果就是像双击退出的那种提示框. 先说说比较简单的吧,通过系统To ...

  5. ML:单变量线性回归(Linear Regression With One Variable)

    模型表达(model regression) 用于描述回归问题的标记 m 训练集(training set)中实例的数量 x 特征/输入变量 y 目标变量/输出变量 (x,y) 训练集中的实例 (x( ...

  6. 推荐一个第三方Qt库的集合 good

    https://inqlude.org/ Stable libraries | Development versions | Unreleased | Commercial | All attica ...

  7. 屏蔽按CapsLock键切换到大写时,编辑框自动弹出的提示(UnregisterClass(TOOLTIPS_CLASS)后,重新设置WndProc并注意返回值)

    WNDPROC OldProc; LPCTSTR lpStr = TEXT("保持大写锁定打开可能会使您错误输入密码"); LRESULT CALLBACK WindowProc( ...

  8. 使用EurekaLog时遇到的问题

    1.在DLL项目中千万不要加入EurekaLog,不然在主程序调用时就会出现莫名其妙的内存问题. 2.要使用EurekaLog发邮件的功能,发邮件的SMTP服务器必须支持8bit MIME编码.如SI ...

  9. REDM基础教程1-下载、编译代码

    1.下载DM REDM的更新路径目前有两个,同步更新,可使用SVN或GIT下载对应代码 https://git.oschina.net/hgy413/REDM https://github.com/h ...

  10. Memcached在Linux系统下的安装和PHP开启 Memcached的 扩展 超级解决方案

    [项目背景]:阿里云ECS服务器,Linux(centos7.2 64位),环境部署使用的是阿里云一键安装包(LAMP)等 [项目需求]:linux安装memcached 和php开启Memcache ...