1.请根据控制台输入的特定日期格式拆分日期 如:请输入一个日期(格式如:月日****年)经过处理得到:****年月日
package string;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String date = sc.nextLine();
int month = date.indexOf("月");
int day = date.indexOf("日");
int year = date.indexOf("年");
String m = date.substring(0,month+1);
String d = date.substring(month+1,day+1);
String y = date.substring(day+1,year+1);
System.out.println(y.concat(m).concat(d));
}
}
2.给出一个随机字符串,判断有多少字母?多少数字?
package string;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] arr = str.toCharArray();
int m = 0;
int n = 0;
for(char c : arr){
if(c >= 'a' && c <= 'z'||c >='A'&&c <= 'Z'){
++m;
}else if(c >= '0' && c <= '9'){
++n;
}
}
System.out.println("字母的个数:"+m+" 数字的个数:"+n);
}
}
以下是一段歌词,请从这段歌词中统计出朋友出现的次数。
“这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。
朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。”;
package string;
public class Demo3 {
public static void main(String[] args) {
String str = "这些年一个人,风也过,雨也走,有过泪," +
"有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首," +
"终有梦终有你在心中。 朋友一生一起走,那些日子不再有,一句话," +
"一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤," +
"还有痛,还要走,还有我。";
/* int startLenth = str.length();
str = str.replace("朋友","");
int endLenth = str.length();
System.out.println((startLenth-endLenth)/"朋友".length());*/ //方法一
int count = 0;
for(int i = 0; i < str.length(); i++){
if(str.indexOf("朋友") >= 0){
str = str.substring(str.indexOf("朋友")+2);
count++;
}
}
System.out.println(count);
}
}
4.编写敏感词过滤程序
说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。
提示:将用户的聊天内容保存到一个字符串对象或一个StringBuilder对象中,然后与敏感词语类表(数组实现)进行比对。如果属于敏感词语,就过滤掉或替换掉。
package string;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String[] regix = {"性","色情","爆炸","恐怖","枪","军火"};
for(String r : regix){
if(str.contains(r)){
str = str.replace(r,"**");
}
}
System.out.println(str);
}
}
5.根据输入的年份、产品类型和随机数产生固定资产编号
即:固定资产编号=年份+0+产品类型+3位随机数
程序运行流程:请输入年份: 请选择产品类型(1. 台式机 2. 笔记本 3. 其他): 生成3位随机数 最后显示固定资产编号
提示:3位随机数按如下方法产生:
(int)(Math.random()*1000);
package string;
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
String year = sc.nextLine();
System.out.println("请选择产品类型(1. 台式机 2. 笔记本 3. 其他)");
String[] type = {"台式机","笔记本","其他"};
int id = sc.nextInt();
int random = (int) (Math.random()*1000);
System.out.println("固定资产编号"+year+type[id-1]+random);
}
}
6.计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。
package string;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Demo6 {
public static void main(String[] args) throws ParseException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个时间:X年X月X日");
String time1 = sc.nextLine();
System.out.println("请输入第二个时间:X年X月X日");
String time2 = sc.nextLine();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
Date date1 = simpleDateFormat.parse(time1);
Date date2 = simpleDateFormat.parse(time2);
long l = 0;
if(date1.after(date2)){
l = date1.getTime()-date2.getTime();
}else{
l = date2.getTime()-date1.getTime();
}
System.out.println("天数:"+l/(1000*3600*24)+" "+"周数:"+l/(1000*3600*24*7));
}
}
7.计算并输出21世纪的闰年,计算程序的执行时间。
package string;
import java.util.GregorianCalendar;
public class Demo7 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
int coount = 0;
for(int i = 2000; i <= 2100; i++){
GregorianCalendar calendar = new GregorianCalendar();
if(calendar.isLeapYear(i)){
System.out.println("闰年:"+i);
}
}
long end = System.currentTimeMillis();
System.out.println("程序所需的时间:" + (end-start)+"毫秒");
}
}
8.编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。、
package string;
import java.util.Scanner;
public class Demo8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
char[] c = str.toCharArray();
for(char cs : c){
if(Character.isLowerCase(cs)){
System.out.print(cs);
}
}
System.out.println();
for(char cs : c){
if(Character.isUpperCase(cs)){
System.out.print(cs);
}
}
}
}
9.编写程序,(Scanner)当以年-月-日的格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几
package string;
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 Demo9 {
public static void main(String[] args) throws ParseException {
System.out.println("输入年月日(年-月-日)");
Scanner sc = new Scanner(System.in);
String date = sc.nextLine();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = simpleDateFormat.parse(date);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date1);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int day = calendar.get(Calendar.DAY_OF_WEEK);
GregorianCalendar gregorianCalendar = new GregorianCalendar();
if(gregorianCalendar.isLeapYear(year)){
System.out.println("闰年");
}else{
System.out.println("不是闰年");
}
System.out.println("本月有"+gregorianCalendar.getMaximum(calendar.DAY_OF_MONTH)+"天");
System.out.println("该日是星期"+day);
}
}