欢迎光临
我们一直在努力

输入身份证号和年份计算年龄

输入一个身份证号码(15位和18位)和一个年份,计算现在的年龄(忽略非法参数)

eg:610618199001020065 2011

输出:21

代码:

package 华为机试题;/** * @author Hutongling * * @time:2017年4月9日 上午10:36:18 */public class 输入身份证号和年份计算年龄 { static void calculateAge(String id,int year){ if(id==null || id.length()!=18 && id.length()!=15) 美国高防vps return ; int length=id.length(); String birthDate=””; if(length==15){ birthDate=id.substring(3,7); }else if(length==18) birthDate=id.substring(6,10); int age=year-Integer.valueOf(birthDate); if(age<0) System.out.println(“输入的年份非法”); else System.out.println(“年龄为: ” + age); } public static void main(String[] args) { String id=”610618199001020065″; calculateAge(id,2011); }}

结果:
年龄为: 21

99274359

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。