有一身份证号,判断此为男还是女,基于此方法,写一个算法,判断此身份证号为男还是女, 身份证的倒数第2位若为偶数是女,反之为男。(身份证分15位和18位)

import java.util.*;

public class p_19_3 {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("请输入您的身份证号: ");
        // 正则表达式
        String IDcard = reader.next();
        String SZ = IDcard.substring(IDcard.length() - 2);// 后二

        String sz = SZ.substring(0, 1);// 后二前一
        try {
            int n1 = Integer.parseInt(sz);// 后二前一转整
            if (IDcard.length() != 18 && IDcard.length() != 15) {
                // 位数
                System.out.println("身份证输入错误,请认真核对您的身份证号111!");
            } else if (n1 % 2 != 0) {
                // 判断奇偶
                System.out.println("根据身份证信息判断,您的性别为男");
            } else {
                System.out.println("根据身份证信息判断,您的性别为女");
            }
        }
        // 出现输入异常
        catch (Exception e) {
            System.out.println("身份证输入错误,请认真核对您的身份证号222!");
        }

    }
}

 

posted @ 2022-05-18 21:33  飞鱼Dai  阅读(342)  评论(0)    收藏  举报