java String、Date、Calendar工具类习题

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/f2764052703/article/details/89218252

一、String习题

1、请根据控制台输入的特定日期格式拆分日期
如:请输入一个日期(格式如:XXXX年XXX月XX日)经过处理得到:XX月XX日XXXX年
提示:使用String的方法indexOf、lastIndexOf、substring

解题思路一:

  1. 使用indexOf()确定“年”、“月”、“日”三个字的位置

  2. 使用substring()将年份、月份、日期分为三个小字符串

  3. 将三个小字符串按照顺序拼接起来,获得一个完整的,有序的日期

    示例代码:

    package test;
    /*
    	1.请根据控制台输入的特定日期格式拆分日期
    
        如:请输入一个日期(格式如:**月**日****年)
        经过处理得到:****年**月**日
    
        提示:使用String的方法indexOf、lastIndexOf、substring
        
        测试数据:2017年5月13日
    */
    
    import java.util.Scanner;
    
    public class test {
    	public static void main(String[] args) {
        	System.out.println("请输入日期:(****年**月**日)");
        	Scanner sc = new Scanner(System.in);
        	String text = sc.nextLine();
        	System.out.println(text);
    
        	int year = text.indexOf("年");
        	int mon = text.indexOf("月");
        	int day = text.indexOf("日");
    
        	String year1 = text.substring(0,text.indexOf("年"))+"年";
        	String mon1 =text.substring(text.indexOf("年")+1,text.indexOf("月"))+"月";
        	String day1 = text.substring(text.indexOf("月")+1,text.indexOf("日"))+"日";
    
        	System.out.println(mon1+day1+year1);
        
    	}
    }
    
    




2、给出一个随机字符串,判断有多少字母?多少数字?

解题思路一:

  1. 将字符串转换为一个字符数组

  2. 将字符数组进行遍历,同时将每一个元素进行判断

  3. 使用两个变量分别统计出现的字母数量和数字数量

    代码示例:

    package test;
    import java.util.Scanner;
    //  2.给出一个随机字符串,判断有多少字母?多少数字?
    
    public class test2 {
    	public static void main(String[] args) {
    	
       		Scanner sc = new Scanner(System.in);
       		String text = sc.nextLine();
       		char[] textCopy = text.toCharArray();
       		
       		int numCount = 0,charCount = 0,elseCount = 0;
       		
       		for (char a : textCopy) {
           		if( a >= '0' && a <= '9'){
               		numCount++;
           		}else if (a >= 'a' && a <= 'z' || a >= 'A' && a <= 'Z'){
               		charCount++;
           		}else{
               		elseCount++;
           		}
       		}
       		
       		System.out.println("字符数量:"+charCount+"\t数字数量:"+numCount+"\t其他字符:"+elseCount);
    	
    	}
    }
    




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

解题思路一:

  1. 使用replace()将“朋友”替换为“”,这样字符串长度会减少

  2. 字符串减少的长度为“朋友”出现的次数的两倍

    代码示例:

    package test;
    /*
    3.以下是一段歌词,请从这段歌词中统计出朋友出现的次数。
        "这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。
        朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
        提示:使用String方法indexOf、substring。
    */
    
    public class test3 {
    	public static void main(String[] args) {
        	String text = "朋友朋友一起走,那些日子不再有,朋友还有我,朋友朋友";
        	int oldLong,newLong,count;
        	oldLong = text.length();
        	text = text.replace("朋友","");
        	newLong = text.length();
        	count = (oldLong-newLong)/2;
        	System.out.println("朋友一共出现:"+count+"次");
    	}
    }
    




4、编写敏感词过滤程序
说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。

解题思路一:

  1. 将敏感词汇写进一个字符串数组中

  2. 循环遍历字符串数组,将语句中的敏感词汇分别使用replace()替换掉

    示例代码:

    package test;
    
    import java.util.Scanner;
    
    /*
    4.编写敏感词过滤程序
        说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。
    
        提示:将用户的聊天内容保存到一个字符串对象或一个StringBuilder对象中,然后与敏感词语类表(数组实现)进行比对。如果属于敏感词语,就过滤掉或替换掉。
    */
    public class test4 {
    	public static void main(String[] args) {
        	String[] weiJin = {"性","色情","爆炸","恐怖","枪","军火"};
        	Scanner sc = new Scanner(System.in);
        	String text = sc.nextLine();
        	for (String old: weiJin) {
            	text = text.replace(old,"***");
        	}
    
        	System.out.println(text);
    	}
    }
    
    




5、根据输入的年份、产品类型和随机数产生固定资产编号
即:固定资产编号=年份+0+产品类型+3位随机数
程序运行流程:请输入年份:
……
请选择产品类型(1. 台式机 2. 笔记本 3. 其他):
……
生成3位随机数
最后显示固定资产编号
提示:3位随机数按如下方法产生:
(int)(Math.random()*1000);

代码示例:

package test;

/*
		根据输入的年份、产品类型和随机数产生固定资产编号
        即:固定资产编号=年份+0+产品类型+3位随机数

        程序运行流程:请输入年份:
        ……
        请选择产品类型(1. 台式机 2. 笔记本 3. 其他):
        ……
        生成3位随机数
        最后显示固定资产编号

        提示:3位随机数按如下方法产生:
        (int)(Math.random()*1000);
*/

import java.util.Scanner;

public class test5 {
    public static void main(String[] args) {
        StringBuffer sb= new StringBuffer();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");
        sb.append(sc.nextLine());
        sb.append(0);
        System.out.println("请输入产品类型:");
        sb.append(sc.nextLine());
        sb.append(String.valueOf((int)(Math.random()*1000)));
        System.out.println(sb);
    }
}



二、Date习题

6、计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。
解题思路一:

  1. 输入要计算的两个日期

  2. 将两个日期转换成时间戳

  3. 将两个时间戳相差的时间转换成日期

    代码示例:

    package test;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Scanner;
    
    //  6.计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。
    public class test6 {
    	public static void main(String[] args) throws ParseException {
    /*       1、输入一个要计算的两个日期
    	      2、将两个日期转换成时间戳
        	  3、将两个时间戳相差的时间转换成日期
    
    		测试数据及测试结果:
          	2017年5月13日   2017-5-13
          	2018年5月13日   2018-5-13
          	2017年5月13日到2018年5月13日相差了:365天	52周
    
         	 2017年5月13日   2017-5-13
         	 2017年7月14日   2017-7-14
         	 2017年5月13日到2017年7月14日相差了:62天	8周
    
    */
    		Scanner sc = new Scanner(System.in);
        	System.out.println("请输入第一个时间:");
        	String date1 = sc.nextLine();
        	System.out.println("请输入第二个时间:");
        	String date2 = sc.nextLine();
    
        	// 自定义日期格式,将输入的日期进行解析
        	SimpleDateFormat sd = new SimpleDateFormat();
        	sd.applyPattern("yyyy年MM月dd日");
        	Date date11 = sd.parse(date1);
        	Date date22 = sd.parse(date2);
    
        	// 将输入的日期转换成时间戳
        	long a =date11.getTime();
        	long b = date22.getTime();
    
        	// 计算两个时间戳之间的差值
        	long c = Math.abs(a-b);
    
        	// 将时间差转换为两个时间相差的天数    1s  =  1000ms
        	long day = c/1000/60/60/24;   // 将毫秒转化为天
        	long week = day/7;
        	long ling = day%7;
    
        	System.out.println(date1+"到"+date2+"相差了:"+day+"天\t"+week+"周"+ling+"天");
    	}
    }
    




7、计算并输出21世纪的闰年,计算程序的执行时间。

解题思路一:

  1. 使用GregorianCalendar类的isLeapYear()直接判断年份是否是闰年

  2. 使用System.currentTimeMillis()来获得程序开始运行和结束运行时间戳,两者相减得到程序运行时间。

    代码示例:

    package test;
    
    import java.util.GregorianCalendar;
    
    // 7.计算并输出21世纪的闰年,计算程序的执行时间。
    public class test7 {
    	public static void main(String[] args) {
        	long beginTime = System.currentTimeMillis(),endTime;
        	GregorianCalendar g = new GregorianCalendar();
        	for (int i = 2000;i<=2100;i++){
            	if(g.isLeapYear(i)){
                	System.out.println(i+"是闰年");
            	}
        	}
        	endTime = System.currentTimeMillis();
        	System.out.println("程序运行的时间为:"+(endTime-beginTime)+"ms");
    	}
    }
    




8、编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。

解题思路一:

  1. 将字符串转换为字符数组

  2. 使用两个StringBuffer分别存储大写和小写字母

  3. 将两个StringBuffer分别打印输出

    示例代码:

    package test;
    
    import java.util.Scanner;
    
    // 8.编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。
    public class test8 {
    	public static void main(String[] args) {
    	
        	Scanner sc = new Scanner(System.in);
        	System.out.println("请输入一个带有大小写字母的字符串");
        	String text = sc.nextLine();
        	
        	char[] text1 = text.toCharArray();
        	StringBuffer supper = new StringBuffer();
        	StringBuffer lower = new StringBuffer();
        	StringBuffer anothor = new StringBuffer();
        	
        	for (char a:text1) {
            	if (a >= 'a' && a <= 'z'){
                	lower.append(a+"、");
            	}else if(a >= 'A' && a <= 'Z'){
                	supper.append(a+"、");
            	}else{
                	anothor.append(a+"、");
            	}
        	}
    
        	System.out.println("大写字母有:"+supper);
        	System.out.println("小写字母有:"+lower);
        	System.out.println("其他字符有:"+anothor);
    
    	}
    }
    



三、Calendar习题

9、编写程序,(Scanner)当以年-月-日的格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几

解题思路一:

  1. 使用SimpleDateFormat来将输入的时间进行格式转化为Date格式

  2. 使用Date初始化一个Calendar

  3. 使用Calendar中的方法获得对应结果

    代码示例:

    package test;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.util.Scanner;
    
    // 9.编写程序,(Scanner)当以年-月-日的格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几
    public class test9 {
    	public static void main(String[] args) throws ParseException {
    
        	Scanner sc = new Scanner(System.in);
        	SimpleDateFormat sd = new SimpleDateFormat();
        	sd.applyPattern("yyyy年MM月dd日");
        	System.out.println("请输入一个日期:");
        	String time = sc.nextLine();
    
        	Date dt = sd.parse(time);
        	Calendar ca = Calendar.getInstance();
        	ca.setTime(dt);
    
        	GregorianCalendar g = new GregorianCalendar();
        	if(g.isLeapYear(ca.get(Calendar.YEAR))){
            	System.out.println(ca.get(Calendar.YEAR)+"年是闰年");
        	}else{
            	System.out.println(ca.get(Calendar.YEAR)+"年不是闰年");
        	}
    
        	System.out.println("这一个月有"+ ca.getActualMaximum(Calendar.DATE) +"天");
    
        	System.out.println("这一天是周"+ (ca.get(Calendar.DAY_OF_WEEK)-1));
        
    	}
    }
    
    

猜你喜欢

转载自blog.csdn.net/f2764052703/article/details/89218252