Java 图形界面设计(二)

Java 实验四 图形界面设计(二)

从文本框输入一个身份证号码,判断是否为正确号码(18位数、最后一位为校验码)。如果是正确号码,输出该人的生日和性别。 附:校验码的公式
=(Σ(ai × wi))%11 其中:i是从右向左2-18位数的序号, wi是第i位上的加权因子 wi =2(i-1) %11 求出
Σ(ai × wi))%11的值,由下表得到效验码a1 公式值 0 1 2 3 4 5 6 7 8 9 10 校验码
1 0 X 9 8 7 6 5 4 3 2

class idCard{
	String id;
	idCard(String ids){
		id=ids;
	}
	public boolean checkFormat(){
		if(id.length()!=18)
			return false;
		char ch;
		for(int i=0;i<17;i++){
			ch=id.charAt(i);
			if(ch<'0'||ch>'9')
				return false;
		}
		ch=id.charAt(17);
		if((ch>='0'&&ch<='9')||ch=='x'||ch=='x')
			return true;
		return false;
	}
	public boolean checkBirthday(){
		int year=Integer.parseInt(id.substring(6,10));
		int month=Integer.parseInt(id.substring(10,12));
		int day=Integer.parseInt(id.substring(12,14));
		if(year>2013||year<2013-130)
			return false;
		if(month<0||month>13)
			return false;
		boolean leapYear=(year%400==0)&&(year%4==0&&year%100!=0);
		if(leapYear){
			int maxDay[]={31,29,31,30,31,30,31,31,30,31,30,31};
			if(day>maxDay[month-1])
				return false;
		}else{
			int maxDay[]={31,28,31,30,31,30,31,31,30,31,30,31};
			if(day>maxDay[month-1])
				return false;
		}
		return true;
	}
	public boolean checkLastBit(){
		int a[]=new int [17];
		for(int i=16;i>=0;i--)
			a[i]=Integer.parseInt(id.charAt(16-i)+"");
		int Wi=1,sum=0,i;
		for(i=0;i<17;i++){
			Wi=(int)(Math.pow(2,i+2-1))%11;
			sum+=a[i]*Wi;
		}
		String lastBit=null;
		switch(sum%11){
			case 0:lastBit="1";break;
			case 1:lastBit="0";break;
			case 2:lastBit="x";break;
			case 3:lastBit="9";break;
			case 4:lastBit="8";break;
			case 5:lastBit="7";break;
			case 6:lastBit="6";break;
			case 7:lastBit="5";break;
			case 8:lastBit="4";break;
			case 9:lastBit="3";break;
			case 10:lastBit="2";break;
		}
		System.out.println("Last bit:"+lastBit);
		if(lastBit.equals(id.substring(17).toLowerCase()))
			return true;
		return false;
	}
	public String getHome(){
		return id.substring(0,6);
	}
	public String getBirthday(){
		return id.substring(6,10)+"-"+id.substring(10,12)+"-"+id.substring(12,14);
	}
	public 	String getSex(){
		if(Integer.parseInt(id.substring(16,17))%2==0)
			return "Female";
		return "Male";
	}
}

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

与我无关-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值