1;
import java.util.Scanner;
/*
* 请根据控制台输入的特定日期格式拆分日期
如:请输入一个日期(格式如:**月**日****年)
经过处理得到:****年**月**日
提示:使用String的方法indexOf、lastIndexOf、substring
*/
public class riqi{
public static void main(String[] args) {
System.out.println("请输入一个日期(格式如:**月**日****年)");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
int ri= str.indexOf('日');获取日的下标
String str1= str.substring(ri+1);截取年份
String str2= str.substring(0, ri+1);//截取月份
System.out.println(str1+str2);
}
}
==================================================================
给出一个随机字符串,判断有多少字母?多少数字?
import java.util.Scanner;
public class lianxi3 {
public static void main(String[] args) {
System.out.println("请在下面输出字母或数字:");
Scanner s = new Scanner(System.in);
String str = s.nextLine();
char[] ch = str.toCharArray();
int shuzi = 0, zimu = 0;
for (int i = 0; i < ch.length; i++) {
if (ch[i] >= '0' && ch[i] <= '9') {
shuzi++;
} else if (ch[i] >= 'a' && ch[i] <= 'z' || ch[i] >= 'A' && ch[i] <= 'Z') {
zimu++;
}
}
System.out.println("字母有:" + zimu + "个");
System.out.println("数字有:" + shuzi + "个");
}
}
=========================================================================
以下是一段歌词,请从这段歌词中统计出朋友出现的次数。
"这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。
朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
提示:使用String方法indexOf、substring。
/*
* 以下是一段歌词,请从这段歌词中统计出朋友出现的次数。
"这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。
朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
提示:使用String方法indexOf、substring。
*/
public class lianxi4 {
public static void main(String[] args) {
String str ="这些年一个人,风也过,雨也走,有过泪,有过错, 还记得坚持甚么,真爱过才会懂,会寂寞会回首,终有梦终有你在心中。朋友一生一起走,那些日子不再有,一句话,一辈子,一生情,一杯酒。朋友不曾孤单过,一声朋友你会懂,还有伤,还有痛,还要走,还有我。";
String [] s = str.split("朋友");
int i =s.length;
System.out.println(i-1);
}
}
==================================================================
/*
* 编写敏感词过滤程序
说明:在网络程序中,如聊天室、聊天软件等,
经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。
如“性”、“色情”、“爆炸”、“恐怖”、“枪”、“军火”等,
这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。
*/
public class lianxi5 {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
String str= s.nextLine();
String [] s1 ={"你大爷","草","傻逼","操你妈"};
for(String ss:s1){
str= str.replace(ss, "**");
}
System.out.println(str);
}
}
============================================================================
public class lianxi6 {
/*
* 使用Math得到一个随机整数:[20,80)
*/
public static void main(String[] args) {
int i= (int)(Math.random()*70+10);
if(i > 20 ||i< 80 ){
System.out.println(i);
}
}
}
===========================================================================
public class lianxi7 {
/*
* 根据输入的年份、产品类型和随机数产生固定资产编号
即:固定资产编号=年份+0+产品类型+3位随机数
程序运行流程:请输入年份:
……
请选择产品类型(1. 台式机 2. 笔记本 3. 其他):
……
生成3位随机数
最后显示固定资产编号
提示:3位随机数按如下方法产生:
(int)(Math.random()*1000);
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
String year = sc.next();
System.out.println("请选择产品类型(1. 台式机 2. 笔记本 3. 其他):");
String id = sc.next();
int i = (int) (Math.random()*900+100);// 0-899 100-999
System.out.println("产品编号为:"+year+id+i);
}
}
===========================================================================
7.
import java.util.Calendar;
import java.util.Scanner;
/**
*
根据用户输入的身份证号码,从字符串中提取用户的出生年月日,输出用户的生日信息。
要求:
身份证号码必须为18位或16位。
年龄范围在1900到当前日期之间。
月份必须在1~12之间
对应的日应该与当前月相符,如4月最多只有30天。注意判断闰年的2月有29天。
* @author 万星明
* @version 1.0
* @time
*/
public class Work5 {
static Scanner sc = new Scanner(System.in);
static Calendar c=Calendar.getInstance();
public static void main(String[] args) {
System.out.println("请输入身份证:");
String IDcard = sc.next();
IDcard(IDcard);
}
public static void IDcard(String IDcard) {
switch(IDcard.length()) {
case 18:
case 16:
String year = IDcard.substring(6, 10);
if(Integer.decode(year) <1900 && Integer.decode(year)>c.get(Calendar.YEAR))
System.out.println("年龄不合法");
String month = IDcard.substring(10,12);
if(Integer.decode(month)<1 && Integer.decode(month)>12)
System.out.println("身份证不合法");
String day = IDcard.substring(12,14);
c.set(Integer.decode(year) ,Integer.decode(month), 0);
if(Integer.decode(day)>c.get(Calendar.DAY_OF_MONTH))
System.out.println("身份证不合法");
System.out.println("尊贵的用户,您的生日为:"+year+"年"+month+"月"+day+"日");
break;
default:
System.out.println("身份证不合法");
break;
}
}
}
===========================================================================
public class lianxi8 {
/*
* 8、编程. 已知字符串:"this is a test of java".
*
* 按要求执行以下操作: (1) 统计该字符串中字母s出现的次数 (2) 取出子字符串"test" (3)
* 用多种方式将本字符串复制到一个字符数组Char[] str中. (4) 将字符串中每个单词的第一个字母变成大写, 并输出到控制台。 (5)
* 用两种方式实现该字符串的倒叙输出。(用StringBuffer和for循环方式分别实现)
*
*/
public static void main(String[] args) {
String str = "this is a test of java";
// (1)统计该字符串中字母s出现的次数
String[] s = str.split("s");
int i = s.length - 1;
System.out.println(i);
// (2) 取出子字符串"test"
int i1 = str.indexOf("test");
String s1 = str.substring(i1, i1 + 4);
System.out.println(s1);
// (3) 用多种方式将本字符串复制到一个字符数组Char[] str中.
char[] ch = str.toCharArray();
for (char c : ch) {
System.out.print(c);
}
System.out.println();
// (4) 将字符串中每个单词的第一个字母变成大写, 并输出到控制台。
String[] strr = str.split(" ");
for (int i11 = 0; i11 < strr.length; i11++) {
char c1 = strr[i11].charAt(0);
c1 = Character.toUpperCase(c1);
System.out.print(c1 + " ");
}
System.out.println();
// (5) 用两种方式实现该字符串的倒叙输出。(用StringBuffer和for循环方式分别实现)
StringBuffer sb = new StringBuffer();
sb.append(str);// 把str 里边的内容添加进来
sb.reverse();// 反转
System.out.println(sb);
char[] ch1 = str.toCharArray();
char ccc;
for (int i2 = 0; i2 < ch.length / 2; i2++) {
ccc = ch[i2];
ch[i2] = ch[ch.length - 1 - i2];
ch[ch.length - 1 - i2] = ccc;
}
for (char c2 : ch1) {
System.out.print(c2 + "");
}
}
}
---------------------
作者:changhe6
来源:CSDN
原文:https://blog.csdn.net/changhe6/article/details/79884393
版权声明:本文为博主原创文章,转载请附上博文链接!