package other;
//输入18位身份证,判断出生日
import java.util.Scanner;
public class IDcard {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("请输入你的身份证:");
String str = sc.next();
sc.close();
String str1 = str.substring(6, 14);
/*substring是string的一个方法,格式为: public String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,一直到索引 endIndex - 1 处
的字符。因此,该子字符串的长度为 endIndex-beginIndex。
示例: "hamburger".substring(4, 8) returns "urge" "smiles".substring(1, 5) returns "mile"
参数: beginIndex - 开始处的索引(包括)。 endIndex - 结束处的索引(不包括)。 返回: 指定的子字符串。
抛出: IndexOutOfBoundsException - 如果 beginIndex 为负,或 endIndex 大于此 String 对象的长
度,或 beginIndex 大于 endIndex。*/
char a[] = str1.toCharArray();//java.lang.String.toCharArray() 方法把这个字符串转换成一个新的字符数组.
System.out.print("您的生日是:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
switch (i) {
case 3://输出截取的字符串第四位后输出年
System.out.print("年");
break;
case 5:输出截取的字符串第四位后输出月
System.out.print("月");
break;
}
}
System.out.print("日");//输出截取的字符串最后一位后输出日
}
}
JAVA学习代码——输入身份证判断生日
最新推荐文章于 2022-10-12 15:27:47 发布