1.请根据控制台输入的特定日期格式拆分日期
如:请输入一个日期(格式如:**月**日****年) 经过处理得到:****年**月**日
提示:使用String的方法indexOf、lastIndexOf、substring
package day01; import java.util.Scanner; /* * 1.请根据控制台输入的特定日期格式拆分日期 如:请输入一个日期(格式如:**月**日****年) 经过处理得到:****年**月**日 提示:使用String的方法indexOf、lastIndexOf、substring */ public class A { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("请输入一个日期(格式如:**月**日****年):"); String str=sc.next(); int beginIndex=str.indexOf("日")+1; int endIndex=str.lastIndexOf("年")+1; String s=str.substring(beginIndex, endIndex);//提取**年 beginIndex=0; endIndex=str.length(); str=s.concat(str);//把 **年 放前边 str=str.substring(beginIndex, endIndex);//提取****年**月**日 System.out.println(str); } }
请输入一个日期(格式如:**月**日****年):
05月20日2019年
2019年05月20日
2.给出一个随机字符串,判断有多少字母?多少数字?
package day01; import java.util.Scanner; /*给出一个随机字符串,判断有多少字母?多少数字?*/ public class B { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("请输入字符串:"); String str=sc.next(); char[] ch=str.toCharArray();//将字符串转换成字符 int letters=0,num=0;//定义计算数字和字母个数的变量 for (char c : ch) { if(Character.isDigit(c)) {//判断是否为数字 num++; }else if(Character.isLetter(c)) {//判断是否为字母 letters++; } } System.out.println(str+"共有"+letters+"个字母,"+num+"个数字。"); } }
请输入字符串:
123456789asdvbh//;''[[
123456789asdvbh//;''[[共有6个字母,9个数字。
3.以下是一段歌词,请从这段歌词中统计出朋友出现的次数。
" 这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。 朋友一生一起走,那些日子不再有, 一 句 话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
提示:使用String方法indexOf、substring。
package day01; /* 3.以下是一段歌词,请从这段歌词中统计出朋友出现的次数。 "这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首, 终有梦终有你在心中。 朋友一生一起走,那些日子不再有, 一 句 话,一辈子,一生情,一杯酒。朋友不曾孤单过, 一声朋友你会懂,还有伤,还有痛,还要走,还有我。"; 提示:使用String方法indexOf、substring。 */ public class C { public static void main(String[] args) { String str="这些年一个人,风也过,雨也走,有过泪,有过错, " + "还记得坚持甚么,真爱过才会懂,会寂寞会回首," + "终有梦终有你在心中。朋友一生一起走,那些日子不再有," + "一 句 话,一辈子,一生情,一杯酒。朋友不曾孤单过," + "一声朋友你会懂,还有伤,还有痛,还要走,还有我。"; int a=0; char[] arr = str.toCharArray(); int i=0; while (i<str.length()) { if(arr[i]=='朋'&&arr[i+1]=='友') { a++; i++; continue; } i++; } System.out.println("朋友出现"+a+"次"); } }
朋友出现3次
4.编写敏感词过滤程序 说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。
提示:将用户的聊天内容保存到一个字符串对象或一个StringBuilder对象中,然后与敏感词语类表(数组实现)进行比对。如果属于敏感词语,就过滤掉或替换掉。
package day01; /* 4.编写敏感词过滤程序 说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。 如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等, 这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉 */ public class D { public static void main(String[] args) { // TODO Auto-generated method stub String[] minganciyu = { "性", "色情", "爆炸", "恐怖", "枪", "军火" }; String str = "恐怖分子有枪,贩卖军火,在闹市制造恐怖袭击;禁止传播色情视频;性行为"; for (int j = 0; j < minganciyu.length; j++) { str = str.replace(minganciyu[j], "*"); } System.out.println(str); } }
*分子有*,贩卖*,在闹市制造*袭击;禁止传播*视频;*行为
5.根据输入的年份、产品类型和随机数产生固定资产编号 即:固定资产编号=年份+0+产品类型+3位随机数
程序运行流程:
请输入年份:
请选择产品类型(1.台式机 2.笔记本 3.其他):
生成3位随机数 最后显示固定资产编号
提示:3位随机数按如下方法产生: (int)(Math.random()*1000);
package day01; import java.util.Scanner; /* 5.根据输入的年份、产品类型和随机数产生固定资产编号 即:固定资产编号=年份+0+产品类型+3位随机数 程序运行流程: 请输入年份: 请选择产品类型(1.台式机 2.笔记本 3.其他): 生成3位随机数 最后显示固定资产编号 提示:3位随机数按如下方法产生: (int)(Math.random()*1000); */ public class E { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("请输入年份:"); int year=sc.nextInt(); System.out.print("请选择产品类型(1.台式机2.笔记本3.其他):"); int product=sc.nextInt(); int random=(int)(Math.random()*1000); int num=year+0+product+random; System.out.println("固定资产编号:"+num); } }
请输入年份:2019
请选择产品类型(1.台式机2.笔记本3.其他):2
固定资产编号:2893
6.计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。
package day01; /* 6.计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。 */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class F { public static void main(String[] args) throws ParseException { String s1 = "2019/06/19"; String s2 = "2000/10/01"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); //把字符串时间解析为Date对象 Date d1 = sdf.parse(s1); Date d2 = sdf.parse(s2); //分别获得两个日期的long类型毫秒数 long date1 = d1.getTime(); long date2 = d2.getTime(); long d = date1 - date2;//两个时间相隔的毫秒数 final long day = 86400000;//一天的毫秒数 System.out.println(s1+"与"+s2+"相隔"+(d/day)+"天,相隔"+(d/day/7)+"周");//相隔多少天,多少周 } }
2019/06/19与2000/10/01相隔6835天,相隔976周
7.计算并输出21世纪的闰年,计算程序的执行时间。
package day01; // 7.计算并输出21世纪的闰年,计算程序的执行时间。 import java.util.GregorianCalendar; public class G { public static void main(String[] args) { int count=0; long start=System.currentTimeMillis(); GregorianCalendar MM=new GregorianCalendar(); for(int i=2000;i<2100;i++) { if(MM.isLeapYear(i)) { count++; } } long end=System.currentTimeMillis(); System.out.println("21世纪的闰年有:"+(count)+"个"); System.out.println("系统执行时间为:"+(end-start)+"ms"); } }
21世纪的闰年有:25个
系统执行时间为:19ms
8.编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。
package day01; //8.编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。 public class H { public static void main(String arg[]) { String s="My name is GuoFeng,I am a boy."; char ch[]; ch=s.toCharArray(); System.out.println("大写字母为:"); for(int i=0;i<ch.length;i++) { if(ch[i]>'A'&&ch[i]<'Z') System.out.print(ch[i]); continue; } System.out.println("\n小写字母为:"); for(int i=0;i<ch.length;i++) { if(ch[i]>'a'&&ch[i]<'z') System.out.print(ch[i]); continue; } } }
大写字母为:
MGFI
小写字母为:
ynmeisuoengmboy
9.编写程序,(Scanner)当以年-月-日的格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几
package day01; /*9.编写程序,(Scanner)当以年-月-日的格式输入一个日期时, 输出其该年是否为闰年,该月有几天,该日是星期几 */ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.Scanner; public class I { public static void main(String[] args) throws ParseException { System.out.println("请输入一个日期:\n(格式为:****年**月**日)"); Scanner scan = new Scanner(System.in); String str = scan.nextLine(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日"); Date d=sdf.parse(str); Calendar c=Calendar.getInstance(); c.setTime(d); int year=c.get(Calendar.YEAR); int month =c.get(Calendar.MONTH)+1; int week =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 month_num =c.getActualMaximum(Calendar.DAY_OF_MONTH);//一个月中最大的天数 System.out.println("该月有"+month_num+"天,"+"该日是周"+week); } }
请输入一个日期:
(格式为:****年**月**日)
2019年6月19日
2019 不是闰年
该月有30天,该日是周3