1.请根据控制台输入的特定日期格式拆分日期
如:请输入一个日期(格式如:**月**日****年)经过处理得到:****年**月**日
1 package test; 2 3 import java.util.Scanner; 4 5 public class num1 { 6 public static void main(String[] args) { 7 8 Scanner c = new Scanner(System.in); 9 System.out.println("请输入日期:(格式如:**月**日****年)"); 10 String str = c.nextLine(); 11 int num = str.indexOf("日"); 12 String str1 = str.substring(num + 1); 13 String str2 = str.substring(0, num + 1); 14 System.out.println(str1 + str2); 15 } 16 }
2.给出一个随机字符串,判断有多少字母?多少数字?
1 package test; 2 3 import java.util.Scanner; 4 5 import javax.xml.stream.events.Characters; 6 7 public class num2 { 8 public static void main(String[] args) { 9 Scanner c = new Scanner(System.in); 10 System.out.println("请输入字符串:"); 11 String str = c.nextLine(); 12 int n = str.length(); 13 int num = 0, let = 0; 14 for (int i = 0; i < n; i++) { 15 if (Character.isDigit(str.charAt(i))) { 16 num++; 17 } 18 if (Character.isLetter(str.charAt(i))) { 19 let++; 20 } 21 } 22 System.out.println("字母:" + let + '\n' + "数字:" + num); 23 } 24 }
3.统计段落中出现某个词的次数
1 package test; 2 3 import java.util.Scanner; 4 5 public class num3 { 6 public static void main(String[] args) { 7 Scanner c = new Scanner(System.in); 8 int count = 0; 9 System.out.println("请输入句子:"); 10 String str = c.nextLine(); 11 System.out.println("请输入要查找的词:"); 12 String card = c.next(); 13 count = word(str, card); 14 System.out.println(card + "在句子中出现" + count); 15 } 16 17 public static int word(String str, String card) { 18 int count = 0, index = 0; 19 while ((index = str.indexOf(card, index)) != -1) { 20 index = index + card.length(); 21 count++; 22 } 23 return count; 24 } 25 }
4.编写敏感词过滤程序
说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。
如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。
1 package test; 2 3 import java.util.Scanner; 4 5 public class num4 { 6 public static void main(String[] args) { 7 Scanner c = new Scanner(System.in); 8 System.out.println("请输入聊天内容:"); 9 String str = c.nextLine(); 10 //StringBuilder str1 = new StringBuilder(str); 11 String ch[] = { "色情", "爆炸", "恐怖", "枪", "军火" }; 12 for (String s : ch) { 13 str = str.replaceAll(s, "*"); 14 } 15 System.out.println("聊天内容:" + str); 16 } 17 }
5.根据输入的年份、产品类型和随机数产生固定资产编号
即:固定资产编号=年份+0+产品类型+3位随机数
程序运行流程:
请输入年份:
请选择产品类型(1.台式机 2.笔记本 3.其他):
生成3位随机数 最后显示固定资产编号
1 package test; 2 3 import java.util.Scanner; 4 5 public class num5 { 6 public static void main(String[] args) { 7 Scanner c = new Scanner(System.in); 8 System.out.println("请输入年份:"); 9 String str = c.nextLine(); 10 System.out.println("请输入产品类型:"); 11 int a = c.nextInt(); 12 int b = (int) (Math.random() * 1000); 13 switch (a) { 14 case 1: 15 System.out.println("固定资产编号:" + str + 0 + "台式机" + b); 16 break; 17 case 2: 18 System.out.println("固定资产编号:" + str + 0 + "笔记本" + b); 19 break; 20 case 3: 21 System.out.println("固定资产编号:" + str + 0 + "其他" + b); 22 break; 23 } 24 } 25 }
6.计算某年、某月、某日和某年、某月、某日之间的天数间隔和周数。
1 package test; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 import java.util.Scanner; 7 8 public class num6 { 9 public static void main(String[] args) throws ParseException { 10 Scanner c = new Scanner(System.in); 11 System.out.println("请输入第一个日期:"); 12 String str = c.nextLine(); 13 System.out.println("请输入第二个日期:"); 14 String str1 = c.nextLine(); 15 SimpleDateFormat str2 = new SimpleDateFormat("yyyy-MM-dd"); 16 Date x = str2.parse(str); 17 Date x1 = str2.parse(str1); 18 long a = 0; 19 if (x.after(x1)) { 20 a = x.getTime() - x1.getTime(); 21 } else { 22 a = x1.getTime() - x.getTime(); 23 } 24 long d = 1000 * 60 * 60 * 24; 25 long day = a / d; 26 long week = day / 7; 27 System.out.println("两个日期相隔" + day + "天"); 28 System.out.println("相隔" + week + "周"); 29 } 30 }
7.计算并输出21世纪的闰年,计算程序的执行时间。
1 package test; 2 3 import java.util.GregorianCalendar; 4 5 public class num7 { 6 public static void main(String[] args) { 7 long start = System.currentTimeMillis(); 8 GregorianCalendar g = new GregorianCalendar(); 9 for (int i = 2000; i < 2100; i++) { 10 if (g.isLeapYear(i)) { 11 System.out.println(i + "是闰年"); 12 } 13 } 14 long end = System.currentTimeMillis(); 15 System.out.println("程序的执行时间为:" + (end - start) + "毫秒"); 16 } 17 }
8.编写一个程序,设定一个有大小写字母的字符串,先将字符串的大写字符输出,再将字符串中的小写字符输出。
1 package test; 2 3 import java.util.Scanner; 4 5 public class num8 { 6 public static void main(String[] args) { 7 Scanner c = new Scanner(System.in); 8 System.out.println("请输入字符串:"); 9 String str = c.nextLine(); 10 char ch[] = str.toCharArray(); 11 System.out.println("大写字母为:"); 12 for (char a : ch) { 13 if (Character.isUpperCase(a)) { 14 System.out.println(a); 15 } 16 } 17 System.out.println("小写字母为:"); 18 for (char a : ch) { 19 if (Character.isLowerCase(a)) { 20 System.out.println(a); 21 } 22 } 23 } 24 }
9.编写程序,当以年-月-日的格式输入一个日期时,输出其该年是否为闰年,该月有几天,该日是星期几
1 package test; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 import java.util.GregorianCalendar; 8 import java.util.Scanner; 9 10 public class num9 { 11 public static void main(String[] args) throws ParseException { 12 Scanner c = new Scanner(System.in); 13 System.out.println("请输入日期:(格式:年-月-日)"); 14 String str = c.nextLine(); 15 SimpleDateFormat str1 = new SimpleDateFormat("yyyy-MM-dd"); 16 Date d = str1.parse(str); 17 Calendar a = Calendar.getInstance(); 18 a.setTime(d); 19 int year = a.get(Calendar.YEAR); 20 int month = a.get(Calendar.MONTH)+1; 21 int week = a.get(Calendar.DAY_OF_WEEK)-1; 22 GregorianCalendar g = new GregorianCalendar(); 23 if (g.isLeapYear(year)) { 24 System.out.println(year + "是闰年"); 25 } else { 26 System.out.println(year + "是平年"); 27 } 28 int max = a.getActualMaximum(Calendar.DAY_OF_MONTH); 29 String s = week == 0 ? "周日" : "周" + week; 30 System.out.println(month + "月有" + max + "天,该日是" + s); 31 } 32 }