- 定义一个方法,根据用户身份证号码计算其年龄并输出。
public class Api1 { public static void main(String[] args) { ID id=new ID(); id.idDefence("xxxxxxxxxxxxxxxxxx");//输入18位身份证号 } } class ID{ public static void idDefence(String s) { String str1=new String(); String str2=new String(); str1=s; int value; int age; if(str1.length()!=18) { System.out.println("身份证输入错误!"); } else { str2=str1.substring(6, 10); value=Integer.parseInt(str2);//将出生年份转换为整型数据 age=2022-value; System.out.println("年龄为:"+age); } } }
实验四 Java API
最新推荐文章于 2024-08-15 14:47:41 发布