(1)输入一个日期(yyyy/mm/dd 或 yyyy-mm-dd),截取年、月、日。
(2)编写程序实现:输入一篇文章,统计该文章中“中国”的出现次数
(3)实现用户注册的验证:用户名、密码、确认密码、身份证、手机号和座机号
a,用户名只能由数字和字母组成,并且长度必须在3~12之间。
b,密码长度必须大于6位,并且密码和确认密码必须一致。
c,身份证号必须是15位或18位,18位身份证号允许最后一位为大写字母X
d,手机号必须是11位数组,并且以1开头。
e,座机号区号由3位数字组成,电话号码为8位数字。029-88888888
程序实现
/*
* 1输入一个日期(yyyy/mm/dd 或 yyyy-mm-dd),截取年、月、日。
*/
public class Date{
public static boolean checkNum(String str){
//遍历字符串元素,并且检查字符串元素是否为数字
for(int i=0;i<str.length();i++){
if(!(str.charAt(i)>='0'&&str.charAt(i)<=9)){
return false;
}
}
return true;
}
public static MyDate get(String date){
if(date.indexOf("-")==4&&date.indexOf("-")==7){
String[]array=date.split("-");
for(int i=0;i<date.length();i++){
if(!checkNum(array[i])){
return null;
}
}
int year=Integer.parseInt(array[0]);
int month=Integer.parseInt(array[1]);
int day=Integer.parseInt(array[3]);
if((year>=1980&&year<=2099)&&(month>=1&&month<=12)&&(day>=1&&day<=31)){
MyDate md=new MyDate();
md.year=year;
md.month=month;
md.day=day;
return md;
}else{
return null;
}
}else{
return null;
}
}
}
----------------------------------------------------------------------------
public class MyDate{
int year;
int month;
int day;
public String toString(){
return "year:"+year+"\nmonth:"+month+"\nday:"+day;
}
}
------------------------------------------------------------------------------
import java.util.Scanner;
public class TestDate{
public static void main(String[] args){
System.out.println("请输入日期 (1996-03-14)");
Scanner sc= new Scanner(System.in);
String date=sc.next();
MyDate md=Date.get(date);
if(md!=null){
System.out.println(md);
}else{
System.out.println("非法日期!");
}
}
}
2)编写程序实现:输入一篇文章,统计该文章中“中国”的出现次数
- 我爱我的中国,我是中国国民,中国
public class CountChina{
public int count(String article){
int count=0;
int index=article.indexOf("中国");
while(index!=-1){
count++;
article=article.substring(index+2);
index=article.indexOf("中国");
}
return count;
}
}
---------------------------------------------------------------
import java.util.Scanner;
public class TestCountChina{
public static void main(String[] args){
System.out.println("请输入一篇关于描述中国的文章");
Scanner sc =new Scanner(System.in);
String str=sc.next();
CountChina cc=new CountChina();
int a=cc.count(str);
System.out.println("中国出现了"+a+"次");
}
}
(3)
public class Register{ //register=注册
public String check(String name,String password,String repassword,String id,String phone,String telphone){
String tishi=null;
for(int i=0;i<name.length();i++){
if(!(((name.charAt(i)>='a'&&name.charAt(i)<='z')||(name.charAt(i)>='A'&&name.charAt(i)<='Z')||(name.charAt(i)>='0'&&name.charAt(i)<='9'))&&(name.length()>=3&&name.length()<=12))){
tishi="你输入的用户名不符合规则";
return tishi;
}
}
System.out.println("----");
if(!((password.length()>6)&&(password.equals(repassword)))){
tishi="密码和确认密码不一致或密码小于6位。";
return tishi;
}else{
//判断id是否满足条件,不满足结束;
if(id.length()==15){
for(int i=0;i<id.length();i++){
if(!(id.charAt(i)>='0'&&id.charAt(i)<='9')){
tishi="id不符合规则";
return tishi;
}
}
}
else if(id.length()==18){
if (!(id.charAt(17)=='x'||(id.charAt(17)>='0'&&id.charAt(17)<='9'))){
tishi="id不符合规则";
return tishi;
}
for(int i=0;i<id.length()-1;i++){
if(!(id.charAt(i)>='0'&&id.charAt(i)<='9')){
tishi="id不符合规则";
return tishi;
}
}
}
//phone的判断
if(phone.length()==11){
for(int i=0;i<name.length();i++){
if(!(phone.charAt(i)>='0'&&phone.charAt(i)<='9')&&(phone.charAt(0)=='1')){
tishi="phone不符合规则";
return tishi;
}
}
}
//telphone的判断
if(telphone.indexOf("-")==3){
for(int i=0;i<=telphone.length();i++){
if(!(telphone.charAt(i)>='0'&&telphone.charAt(i)<='9')){
tishi="telphone不符合规则";
return tishi;
}
else{
tishi="注册成功";
return tishi;
}
}
}else{
tishi="telphone不符合规则";
return tishi;
}
return tishi;
}
}
}
----------------------------------------------------------------------------------
import java.util.Scanner;
public class TestRegister{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
System.out.println("输入用户名");
String name=sc.next();
System.out.println("输入密码");
String pas=sc.next();
System.out.println("确认密码");
String repas=sc.next();
System.out.println("输入身份证号");
String id=sc.next();
System.out.println("输入手机号");
String phone=sc.next();
System.out.println("输入座机号");
String telphone=sc.next();
Register rg=new Register();
String result=rg.check(name,pas,repas,id,phone,telphone);
System.out.println(result);
}
}