JAVA API方法的应用

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

Scanner c=new Scanner(System.in);
String date=c.next();
int year,month,day;
year=date.indexOf("年",0);
month=date.indexOf("月",0);
day=date.indexOf("日",0);
String date1=date.substring(day+1,year+1)+date.substring(0,month+1)+date.substring(month+1,day+1);
System.out.println(date1);

3月21日2019年
2019年3月21日

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

Scanner c=new Scanner(System.in);
String date=c.next();
char[] date1=date.toCharArray();
int letter=0;
int num=0;
for (char a:date1){
    if(a>='0'&&a<='9'){
        num++;
    }
    if(a>='a'&&a<='z'){
        letter++;
    }
    if(a>='A'&&a<='Z'){
        letter++;
    }
}
System.out.println("字母的个数是"+letter);
System.out.println("数字的个数是"+num);

sadgf2345
字母的个数是5
数字的个数是4

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

public static void main(String[] args) {
    String a="朋友这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
    int num=0;
    b(a,num);
}
public static void b(String a,int num){
    int n=a.indexOf("朋友");
    if(n>=0){
        num++;
        b(a.substring(n+2),num);
    }else {
        System.out.println("朋友出现的次数是"+num);
    }
    exit(0);
}

朋友出现的次数是4

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

Scanner a=new Scanner(System.in);
String str=a.next();
String[] str1={"性","色情","爆炸","恐怖","枪","军火"};
String nstr="**";
String q;
for(String b:str1){
    q= str.replace(b,nstr);
    if(b.equals("军火")){
        System.out.println(q);
    }
    str=q;
}

军火,是色情
**,是**

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

Scanner s=new Scanner(System.in);
int a= Integer.parseInt(s.next());
int b=Integer.parseInt(s.next());
double q=Math.random();
int c=(int)(Math.random()*1000);
//应当注意不要写成(int)Math.radom()*1000少一个括号输出的就是0,这里返回的是一个double类型的数
System.out.println(a*10000+b*1000+c);

3
6
36124

6.计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。
Scanner sc=new Scanner(System.in);
String one=sc.nextLine();
String two=sc.nextLine();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
Date d1=sdf.parse(one);
Date d2=sdf.parse(two);
Long date=Math.abs((d1.getTime()-d2.getTime())/(1000*24*60*60));
System.out.println(date);
System.out.println(date/7);

2017年3月4日
2017年3月5日
1
0

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

long startime=System.nanoTime();
GregorianCalendar calendar = new GregorianCalendar();
for(int i=2000;i<2100;i++){
    if(calendar.isLeapYear(i)){
        System.out.print(i+" ");
    }
}
System.out.println();
long endtime=System.nanoTime();
System.out.println(endtime-startime+"ns");

2000 2004 2008 2012 2016 2020 2024 2028 2032 2036 2040 2044 2048 2052 2056 2060 2064 2068 2072 2076 2080 2084 2088 2092 2096
146429718ns

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

Scanner sc=new Scanner(System.in);
    String a=sc.next();
    char[] b=a.toCharArray();
    for(char c:b){
        if(isLowerCase(c)){
            System.out.print(c);
        }
    }
    System.out.println();
    for (char c:b){
        if(isUpperCase(c)){
            System.out.print(c);
        }
    }
}

JKLJasgjKLJKNJKLJLfjgklasd
asgjfjgklasd
JKLJKLJKNJKLJL

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

Scanner sc=new Scanner(System.in);
String date=sc.next();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
Date date1=sdf.parse(date);

Calendar cl=  Calendar.getInstance();
cl.setTime(date1);
GregorianCalendar gc=new GregorianCalendar();
if(gc.isLeapYear(cl.YEAR)){
    System.out.println("闰年");
}else {
    System.out.println("不是闰年");
}
System.out.println("这个月有"+cl.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println("这是星期"+(cl.get(Calendar.DAY_OF_WEEK)-1));

2019年4月10日
不是闰年
这个月有30
这是星期3

猜你喜欢

转载自blog.csdn.net/MCYZSF/article/details/89188022