Java常用工具类习题

1.请根据控制台输入的特定日期格式拆分日期
如:请输入一个日期(格式如:日****年)
经过处理得到:****年

package java1;

        import java.util.Date;
        import java.util.Scanner;

public class Ja1 {
    public static void main(String[] args) {
        Scanner si=new Scanner(System.in);
        System.out.printf("请输入一个日期:");
        String riqi=si.nextLine();
        String s1=riqi.substring(riqi.indexOf("年")-4,riqi.indexOf("年"));
        String s2=riqi.substring(riqi.indexOf("月")-2,riqi.indexOf("月"));
        String s3=riqi.substring(riqi.indexOf("日")-2,riqi.indexOf("日"));
        System.out.println("经过处理得到:"+s1+"年"+s2+"月"+s3+"日");
    }
}

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

package java1;

public class Ja2 {
    public static void main(String[] args) {
        String sz = "dsdsad54ds6a4dasdsa";
        int i=0;
        int j=0;
        char[] cs=sz.toCharArray();/*把字符串转为字符数组;*/
        for (char c : cs) {
            if(Character.isDigit(c)){/*判断是否为数字*/
                i++;
            }else if(Character.isLetter(c)){/*判断是否为字母*/
                j++;
            }
        }
        System.out.println("一共有"+i+"个数字,"+j+"个字母");
    }
}

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

package java1;

public class Ja3 {
    public static void main(String[] args) {
        String s1="朋友这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂," +
                "会寂寞会回首,终有梦终有你在心中。"
                +"朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过," +
                "一声朋友你会懂,还有伤,还有痛朋友,还要走,还有我。";

       /* String[] s2=s1.split("朋友");*//*根据指定的规则拆分字符串,返回字符串数组*//*
        System.out.println(s2.length-1);*/
        String key="朋友";
        int startLen= s1.length();
        s1= s1.replace(key,"");
        int endLen= s1.length();
        int icount=(startLen-endLen)/key.length();
        System.out.println(key+"一共出现了"+icount+"次");
    }
}

4.编写敏感词过滤程序
说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。
提示:将用户的聊天内容保存到一个字符串对象或一个StringBuilder对象中,然后与敏感词语类表(数组实现)进行比对。如果属于敏感词语,就过滤掉或替换掉。

package java1;

import java.util.Scanner;

public class Ja4 {
    public static void main(String[] args) {
        String[] s1={"性", "色情", "爆炸", "恐怖", "枪", "军火", "法轮功"};
        Scanner sc=new Scanner(System.in);
        String in=sc.nextLine();
        System.out.println(in);
        for (String s2:s1)
        {
            String a1="#";
            if (in.contains(s2))
            {
                in=in.replace(s2,a1);
            }
        }
        System.out.println(in);
    }
}

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

package java1;

import java.util.Random;
import java.util.Scanner;

public class Ja5 {
    public static void main(String[] args) {
        System.out.println("请输入年份:");
        Scanner sc=new Scanner(System.in);
        String a=sc.nextLine();
        System.out.println("请选择产品类型(1. 台式机 2. 笔记本 3. 其他):");
        String s1=sc.nextLine();
        Random ran=new Random();
        int b=ran.nextInt(900)+100;
        System.out.println("产品编号为:" + a + "年"+"0" + s1 + b);
    }
}

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

package java1;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Ja6 {
    public static void main(String[] args) throws ParseException {
        SimpleDateFormat da1=new SimpleDateFormat("yyyy-MM-dd");
        Scanner s=new Scanner(System.in);
        System.out.println("请输入第一个日期(yyyy-MM-dd):");
        String a1=s.nextLine();
        Date d=da1.parse(a1);
        System.out.println(d);
        long b=d.getTime();
        System.out.println(b);
        System.out.println("请输入第二个日期(yyyy-MM-dd):");
        String a2=s.nextLine();
        Date d1=da1.parse(a2);/*字符串转化为时间的形式*/
        long b1=d1.getTime();/*获得时间戳*/
        System.out.println(b1);
        long c=0;
        if (b>b1)
        {
            c=b-b1;
        }else if(b<b1){
            c=b1-b;
        }else
        {
            c=0;
        }
        System.out.println(c);
        long n=c/86400000;
        System.out.println("天数间隔为"+n+"天");
        long m=c/604800000;
        System.out.println("周数间隔为"+m+"周");
    }
}

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

package java1;

import java.util.GregorianCalendar;

public class Ja7 {
    public static void main(String[] args) {
        long begin=System.currentTimeMillis();/*返回以毫秒为单位的当前时间。*/
        GregorianCalendar a=new GregorianCalendar();
        for (int i=2000;i<2100;i++)
        {
            if(a.isLeapYear(i))/*判断指定年份是否为闰年,是闰年返回true*/
            {
                System.out.println(i+"是闰年");
            }
        }
        long end=System.currentTimeMillis();/*返回以毫秒为单位的当前时间。*/
        System.out.println("计算机执行的时间是:"+(end-begin)+"毫秒");
    }
}

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

package java1;

import java.util.Scanner;

public class Ja8 {
    public static void main(String[] args) {
        Scanner sv=new Scanner(System.in);
        System.out.println("请输入字符串");
        String a=sv.nextLine();
        char b[]=a.toCharArray();
        for(char c:b)
        {
            if(Character.isUpperCase(c))/*判断是否为大写字母*/
            {
                System.out.printf(c+" ");
            }
        }
        System.out.println("");
        for(char c:b)
        {
            if(Character.isLowerCase(c))/*判断是否为大写字母*/
            {
                System.out.printf(c+" ");
            }
        }
    }
}

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

package java1;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Ja9 {
    public static void main(String[] args) throws ParseException {
        System.out.println("输入一个日期如:(yyyy-MM-dd)");
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sd.parse(str);/*字符串转化为时间的形式*/
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        int year = c.get(Calendar.YEAR);
        int yue = c.get(Calendar.MONTH) + 1;
        int zhou = c.get(Calendar.DAY_OF_WEEK) - 1;
        GregorianCalendar g = new GregorianCalendar();
        if (g.isLeapYear(year)) {
            System.out.println(year + "是闰年");
        } else {
            System.out.println(year + "是平年");
        }
        int max = c.getActualMaximum(Calendar.DAY_OF_MONTH);//一个月中最大的天数
        if (zhou == 0) {
            System.out.println(yue + "月有" + max + "天" + "该日是" + "周日");
        } else {
            System.out.println(yue + "月有" + max + "天" + "该日是" + "周" + zhou);
        }
    }
}


猜你喜欢

转载自blog.csdn.net/qq_41556688/article/details/89190101