Java常用工具类练习题

1.请根据控制台输入的特定日期格式拆分日期

如:请输入一个日期(格式如: ** 月 ** 日 **** 年)
经过处理得到: **** 年 ** 月 ** 日
提示:使用String的方法indexOf、lastIndexOf、substring

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int i = str.indexOf("日") + 1;
        String last = new String(str.substring(i) + str.substring(0,i));
        System.out.println(last);


    }

在这里插入图片描述

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

  public static String getStr(int len){
        String str = "qazwsxedcrfvtgbyhnujmikolpCRFVTGBYHNUJMIKOLP0123456789";
        Random ran = new Random();
        String sj = "";
        for(int i = 0;i < len; i++){
            int j = ran.nextInt(str.length());
            char s = str.charAt(j);
            sj += s;
        }
        return sj;
    }

    public static void main(String[] args) {

        String str = Test2.getStr(10);
        System.out.println(str);
        char[] cs = str.toCharArray();
        int i = 0;
        int j = 0;
        for (char c : cs){
            if(Character.isDigit(c)){
                i++;
            }else if (Character.isLetter(c)){
                j++;
            }
        }
        System.out.println("一共" + j + "个字母," + i + "个数字");
    }

在这里插入图片描述

3.以下是一段歌词,请从这段歌词中统计出朋友出现的次数。

"这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。
朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";

提示:使用String方法indexOf、substring。

public static void main(String[] args) {

        String str = "这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
        int i = 0;
        while (true){
            str = str.substring(str.indexOf("朋友")+1);
            i++;
            if (str.indexOf("朋友")<0){
                break;
            }
        }
        System.out.println(i);
    }

在这里插入图片描述

4.编写敏感词过滤程序

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

提示:将用户的聊天内容保存到一个字符串对象或一个StringBuilder对象中,然后与敏感词语类表(数组实现)进行比对。如果属于敏感词语,就过滤掉或替换掉。

public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String sb = sc.next();
        String[] sts = {"性","色情","爆炸","恐怖","枪","军火"};
        for (String st : sts){
            sb = sb.replace(st, "*");
        }
        System.out.println(sb);
    }

在这里插入图片描述

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

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

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

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

public static void main(String[] args) {

        System.out.println("请输入年份:");
        Scanner sc = new Scanner(System.in);
        String st = sc.next();
        System.out.println("请输入产品类型(1.台式机 2.笔记本 3.其他)");
        String st1 = sc.next();
        String la = st + "0" + st1+ (int)(Math.random()*10) + (int)(Math.random()*10) +(int)(Math.random()*10);
        System.out.println(la);
    }

在这里插入图片描述

6.计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。

public static void main(String[] args) throws ParseException {

        System.out.println("请输入第一个日期(yyyy-mm-dd):");
        Scanner sc = new Scanner(System.in);
        String st = sc.next();
        System.out.println("请输入第二个日期(yyyy-mm-dd):");
        String st2 = sc.next();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sd.parse(st);
        Date d2 = sd.parse(st2);
        long l = 0;
        if(d.after(d2)){
            l = d.getTime()-d2.getTime();
        }
        else {
            l = d2.getTime()-d.getTime();
        }
        long a = 1000*60*60*24;
        long day = l/a;
        long week = day/7;
        System.out.println("两个日期相隔"+day+"天");
        System.out.println("相隔"+week+"周");
    }

在这里插入图片描述

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

 public static void main(String[] args) {

        long startTime = System.currentTimeMillis();
        System.out.println("21世纪闰年:");
        GregorianCalendar gc = new GregorianCalendar();
        for (int i = 2000; i < 2100; i++){
            if (gc.isLeapYear(i)){
                System.out.println(i);
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
    }

在这里插入图片描述

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

public static String getStr(int len){
        String str = "qazwsxedcrfvtgbyhnujmikolpCRFVTGBYHNUJMIKOLP";
        Random ran = new Random();
        String sj = "";
        for(int i = 0;i < len; i++){
            int j = ran.nextInt(str.length());
            char s = str.charAt(j);
            sj += s;
        }
        return sj;
    }

    public static void main(String[] args) {

        String str = Test8.getStr(10);
        System.out.println(str);
        char[] cs = str.toCharArray();
        String d = new String();
        String x = new String();
        for (char c : cs){
            if (Character.isLowerCase(c)){
                x += c;
            }else if (Character.isUpperCase(c)){
                d += c;
            }
        }
        System.out.println("大写:" + d);
        System.out.println("小写: " + x);
    }

在这里插入图片描述

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

public static void main(String[] args) throws ParseException {

        System.out.println("请输入日期(yyyy-mm-dd):");
        Scanner sc = new Scanner(System.in);
        String st = sc.next();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sd.parse(st);
        Calendar cl = Calendar.getInstance();
        int y = cl.get(Calendar.YEAR);
        int m = cl.get(Calendar.MONTH);
        int w = cl.get(Calendar.DAY_OF_WEEK) - 1;
        GregorianCalendar gc = new GregorianCalendar();
        int d = gc.getActualMaximum(Calendar.DAY_OF_MONTH);
        if(gc.isLeapYear(y)){
            System.out.println("闰年");
        }else {
            System.out.println("不是闰年");
        }
        System.out.println(d + "天");
        System.out.println("星期" + w);
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值