[software development] 身份证号码生成器
Tofloor
poster avatar
199******64
deepin
2023-10-04 07:04
Author

因为我这边是996.所以我只能晚上学习Java,这个是我学习了一年多之后,今天自己写的一个身份证号码生成代码,请大家给点优化意见。代码在txt文档里面

image.png

image.png

新建文本文档.txt
image.png

Reply Favorite View the author
All Replies
落日
deepin
2023-10-04 07:21
#1

改用正则表达式比较方便吧

Reply View the author
raspbian
deepin
2023-10-04 16:46
#2

这种大段的if else应该换成switch case

这种写法不像是已经学了一年的

Reply View the author
199******64
deepin
2023-10-04 17:21
#3
raspbian

这种大段的if else应该换成switch case

这种写法不像是已经学了一年的

switch-case我也想过!主要是觉得那段if~else即使用case代码也显得又臭也很长!目前还没找到解决思路!

Reply View the author
raspbian
deepin
2023-10-04 17:46
#4

稍微好一点

func IdCard() {
    Area := []string{"230602", "230604"}
    Year := []string{"1967", "1970"}
    Month := []string{
        "01",
        "02",
        "03",
        "04",
        "05",
        "06",
        "07",
        "08",
        "09",
        "10",
        "11",
        "12",
    }
    Day := []string{
        "01",
        "02",
        "03",
        "04",
        "05",
        "06",
        "07",
        "08",
        "09",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23",
        "24",
        "25",
        "26",
        "27",
        "28",
    }
    randNum := strconv.Itoa(rand.Intn(1999-100+1) + 100)
    for i := 0; i < len(Area); i++ {
        for j := 0; j < len(Year); j++ {
            for p := 0; p < len(Month); p++ {
                for q := 0; q < len(Day); q++ {
                    stand := strings.Join([]string{Area[i], Year[j], Month[p], Day[q], randNum}, "")
                    i1, _ := strconv.Atoi(strconv.Itoa(int(stand[0])))
                    i2, _ := strconv.Atoi(strconv.Itoa(int(stand[1])))
                    i3, _ := strconv.Atoi(strconv.Itoa(int(stand[2])))
                    i4, _ := strconv.Atoi(strconv.Itoa(int(stand[3])))
                    i5, _ := strconv.Atoi(strconv.Itoa(int(stand[4])))
                    i6, _ := strconv.Atoi(strconv.Itoa(int(stand[5])))
                    i7, _ := strconv.Atoi(strconv.Itoa(int(stand[6])))
                    i8, _ := strconv.Atoi(strconv.Itoa(int(stand[7])))
                    i9, _ := strconv.Atoi(strconv.Itoa(int(stand[8])))
                    i10, _ := strconv.Atoi(strconv.Itoa(int(stand[9])))
                    i11, _ := strconv.Atoi(strconv.Itoa(int(stand[10])))
                    i12, _ := strconv.Atoi(strconv.Itoa(int(stand[11])))
                    i13, _ := strconv.Atoi(strconv.Itoa(int(stand[12])))
                    i14, _ := strconv.Atoi(strconv.Itoa(int(stand[13])))
                    i15, _ := strconv.Atoi(strconv.Itoa(int(stand[14])))
                    i16, _ := strconv.Atoi(strconv.Itoa(int(stand[15])))
                    i17, _ := strconv.Atoi(strconv.Itoa(int(stand[16])))
                    i18 := (i1*7 + i2*9 + i3*10 + i4*5 + i5*8 + i6*4 + i7*2 + i8*1 + i9*6 + i10*3 + i11*7 + i12*9 + i13*10 + i14*5 + i15*8 + i16*4 + i17*2) % 11
                    switch i18 {
                        case 0:
                        fmt.Println(strings.Join([]string{stand, "1"}, ""))
                        case 1:
                        fmt.Println(strings.Join([]string{stand, "0"}, ""))
                        case 2:
                        fmt.Println(strings.Join([]string{stand, "X"}, ""))
                        case 3:
                        fmt.Println(strings.Join([]string{stand, "9"}, ""))
                        case 4:
                        fmt.Println(strings.Join([]string{stand, "8"}, ""))
                        case 5:
                        fmt.Println(strings.Join([]string{stand, "7"}, ""))
                        case 6:
                        fmt.Println(strings.Join([]string{stand, "6"}, ""))
                        case 7:
                        fmt.Println(strings.Join([]string{stand, "5"}, ""))
                        case 8:
                        fmt.Println(strings.Join([]string{stand, "4"}, ""))
                        case 9:
                        fmt.Println(strings.Join([]string{stand, "3"}, ""))
                        case 10:
                        fmt.Println(strings.Join([]string{stand, "2"}, ""))
                    }
                }
            }
        }
    }
}
Reply View the author
199******64
deepin
2023-10-04 18:17
#5
It has been deleted!
199******64
deepin
2023-10-04 18:19
#6
raspbian

稍微好一点

func IdCard() {
    Area := []string{"230602", "230604"}
    Year := []string{"1967", "1970"}
    Month := []string{
        "01",
        "02",
        "03",
        "04",
        "05",
        "06",
        "07",
        "08",
        "09",
        "10",
        "11",
        "12",
    }
    Day := []string{
        "01",
        "02",
        "03",
        "04",
        "05",
        "06",
        "07",
        "08",
        "09",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23",
        "24",
        "25",
        "26",
        "27",
        "28",
    }
    randNum := strconv.Itoa(rand.Intn(1999-100+1) + 100)
    for i := 0; i < len(Area); i++ {
        for j := 0; j < len(Year); j++ {
            for p := 0; p < len(Month); p++ {
                for q := 0; q < len(Day); q++ {
                    stand := strings.Join([]string{Area[i], Year[j], Month[p], Day[q], randNum}, "")
                    i1, _ := strconv.Atoi(strconv.Itoa(int(stand[0])))
                    i2, _ := strconv.Atoi(strconv.Itoa(int(stand[1])))
                    i3, _ := strconv.Atoi(strconv.Itoa(int(stand[2])))
                    i4, _ := strconv.Atoi(strconv.Itoa(int(stand[3])))
                    i5, _ := strconv.Atoi(strconv.Itoa(int(stand[4])))
                    i6, _ := strconv.Atoi(strconv.Itoa(int(stand[5])))
                    i7, _ := strconv.Atoi(strconv.Itoa(int(stand[6])))
                    i8, _ := strconv.Atoi(strconv.Itoa(int(stand[7])))
                    i9, _ := strconv.Atoi(strconv.Itoa(int(stand[8])))
                    i10, _ := strconv.Atoi(strconv.Itoa(int(stand[9])))
                    i11, _ := strconv.Atoi(strconv.Itoa(int(stand[10])))
                    i12, _ := strconv.Atoi(strconv.Itoa(int(stand[11])))
                    i13, _ := strconv.Atoi(strconv.Itoa(int(stand[12])))
                    i14, _ := strconv.Atoi(strconv.Itoa(int(stand[13])))
                    i15, _ := strconv.Atoi(strconv.Itoa(int(stand[14])))
                    i16, _ := strconv.Atoi(strconv.Itoa(int(stand[15])))
                    i17, _ := strconv.Atoi(strconv.Itoa(int(stand[16])))
                    i18 := (i1*7 + i2*9 + i3*10 + i4*5 + i5*8 + i6*4 + i7*2 + i8*1 + i9*6 + i10*3 + i11*7 + i12*9 + i13*10 + i14*5 + i15*8 + i16*4 + i17*2) % 11
                    switch i18 {
                        case 0:
                        fmt.Println(strings.Join([]string{stand, "1"}, ""))
                        case 1:
                        fmt.Println(strings.Join([]string{stand, "0"}, ""))
                        case 2:
                        fmt.Println(strings.Join([]string{stand, "X"}, ""))
                        case 3:
                        fmt.Println(strings.Join([]string{stand, "9"}, ""))
                        case 4:
                        fmt.Println(strings.Join([]string{stand, "8"}, ""))
                        case 5:
                        fmt.Println(strings.Join([]string{stand, "7"}, ""))
                        case 6:
                        fmt.Println(strings.Join([]string{stand, "6"}, ""))
                        case 7:
                        fmt.Println(strings.Join([]string{stand, "5"}, ""))
                        case 8:
                        fmt.Println(strings.Join([]string{stand, "4"}, ""))
                        case 9:
                        fmt.Println(strings.Join([]string{stand, "3"}, ""))
                        case 10:
                        fmt.Println(strings.Join([]string{stand, "2"}, ""))
                    }
                }
            }
        }
    }
}
public class identityCardTwo {
    public static void main(String[] args) {
        String[] IDSixdigit = {"610527", "610528","610529"};
        String[] IDYear = {"1991", "1992"};
        String[] IDMonth = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        String[] IDDay = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "22", "22", "23", "24", "25", "26", "27", "28"};
        String randomNumber = String.valueOf((long) ((Math.random() * (999 - 100 + 1)) + 100));

        for (int p = 0; p < IDSixdigit.length; p++) {
            for (int l = 0; l < IDYear.length; l++) {
                for (int d = 0; d < IDMonth.length; d++) {
                    for (int q = 0; q < IDDay.length; q++) {
                        String s1 = IDSixdigit[p] + IDYear[l] + IDMonth[d] + IDDay[q] + randomNumber;
                        long l1 = Long.parseLong(s1.substring(0, 1)) * 7;
                        long l2 = Long.parseLong(s1.substring(1, 2)) * 9;
                        long l3 = Long.parseLong(s1.substring(2, 3)) * 10;
                        long l4 = Long.parseLong(s1.substring(3, 4)) * 5;
                        long l5 = Long.parseLong(s1.substring(4, 5)) * 8;
                        long l6 = Long.parseLong(s1.substring(5, 6)) * 4;
                        long l7 = Long.parseLong(s1.substring(6, 7)) * 2;
                        long l8 = Long.parseLong(s1.substring(7, 8)) * 1;
                        long l9 = Long.parseLong(s1.substring(8, 9)) * 6;
                        long l10 = Long.parseLong(s1.substring(9, 10)) * 3;
                        long l11 = Long.parseLong(s1.substring(10, 11)) * 7;
                        long l12 = Long.parseLong(s1.substring(11, 12)) * 9;
                        long l13 = Long.parseLong(s1.substring(12, 13)) * 10;
                        long l14 = Long.parseLong(s1.substring(13, 14)) * 5;
                        long l15 = Long.parseLong(s1.substring(14, 15)) * 8;
                        long l16 = Long.parseLong(s1.substring(15, 16)) * 4;
                        long l17 = Long.parseLong(s1.substring(16, 17)) * 2;

                        long l18 = (l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + l9 + l10 + l11 + l12 + l13 + l14 + l15 + l16 + l17) % 11;

                        switch ((int) l18){
                            case 0:
                                System.out.println(s1 + "1");
                                break;
                            case 1:
                                System.out.println(s1 + "0");
                                break;
                            case 2:
                                System.out.println(s1 + "X");
                                break;
                            case 3:
                                System.out.println(s1 + "9");
                                break;
                            case 4:
                                System.out.println(s1 + "8");
                                break;
                            case 5:
                                System.out.println(s1 + "7");
                                break;
                            case 6:
                                System.out.println(s1 + "6");
                                break;
                            case 7:
                                System.out.println(s1 + "5");
                                break;
                            case 8:
                                System.out.println(s1 + "4");
                                break;
                            case 9:
                                System.out.println(s1 + "3");
                                break;
                            default:
                                System.out.println(s1 + "2");
                                break;

                        }

                    }
                }
            }
        }

    }

Reply View the author
199******64
deepin
2023-10-04 18:20
#7
raspbian

稍微好一点

func IdCard() {
    Area := []string{"230602", "230604"}
    Year := []string{"1967", "1970"}
    Month := []string{
        "01",
        "02",
        "03",
        "04",
        "05",
        "06",
        "07",
        "08",
        "09",
        "10",
        "11",
        "12",
    }
    Day := []string{
        "01",
        "02",
        "03",
        "04",
        "05",
        "06",
        "07",
        "08",
        "09",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19",
        "20",
        "21",
        "22",
        "23",
        "24",
        "25",
        "26",
        "27",
        "28",
    }
    randNum := strconv.Itoa(rand.Intn(1999-100+1) + 100)
    for i := 0; i < len(Area); i++ {
        for j := 0; j < len(Year); j++ {
            for p := 0; p < len(Month); p++ {
                for q := 0; q < len(Day); q++ {
                    stand := strings.Join([]string{Area[i], Year[j], Month[p], Day[q], randNum}, "")
                    i1, _ := strconv.Atoi(strconv.Itoa(int(stand[0])))
                    i2, _ := strconv.Atoi(strconv.Itoa(int(stand[1])))
                    i3, _ := strconv.Atoi(strconv.Itoa(int(stand[2])))
                    i4, _ := strconv.Atoi(strconv.Itoa(int(stand[3])))
                    i5, _ := strconv.Atoi(strconv.Itoa(int(stand[4])))
                    i6, _ := strconv.Atoi(strconv.Itoa(int(stand[5])))
                    i7, _ := strconv.Atoi(strconv.Itoa(int(stand[6])))
                    i8, _ := strconv.Atoi(strconv.Itoa(int(stand[7])))
                    i9, _ := strconv.Atoi(strconv.Itoa(int(stand[8])))
                    i10, _ := strconv.Atoi(strconv.Itoa(int(stand[9])))
                    i11, _ := strconv.Atoi(strconv.Itoa(int(stand[10])))
                    i12, _ := strconv.Atoi(strconv.Itoa(int(stand[11])))
                    i13, _ := strconv.Atoi(strconv.Itoa(int(stand[12])))
                    i14, _ := strconv.Atoi(strconv.Itoa(int(stand[13])))
                    i15, _ := strconv.Atoi(strconv.Itoa(int(stand[14])))
                    i16, _ := strconv.Atoi(strconv.Itoa(int(stand[15])))
                    i17, _ := strconv.Atoi(strconv.Itoa(int(stand[16])))
                    i18 := (i1*7 + i2*9 + i3*10 + i4*5 + i5*8 + i6*4 + i7*2 + i8*1 + i9*6 + i10*3 + i11*7 + i12*9 + i13*10 + i14*5 + i15*8 + i16*4 + i17*2) % 11
                    switch i18 {
                        case 0:
                        fmt.Println(strings.Join([]string{stand, "1"}, ""))
                        case 1:
                        fmt.Println(strings.Join([]string{stand, "0"}, ""))
                        case 2:
                        fmt.Println(strings.Join([]string{stand, "X"}, ""))
                        case 3:
                        fmt.Println(strings.Join([]string{stand, "9"}, ""))
                        case 4:
                        fmt.Println(strings.Join([]string{stand, "8"}, ""))
                        case 5:
                        fmt.Println(strings.Join([]string{stand, "7"}, ""))
                        case 6:
                        fmt.Println(strings.Join([]string{stand, "6"}, ""))
                        case 7:
                        fmt.Println(strings.Join([]string{stand, "5"}, ""))
                        case 8:
                        fmt.Println(strings.Join([]string{stand, "4"}, ""))
                        case 9:
                        fmt.Println(strings.Join([]string{stand, "3"}, ""))
                        case 10:
                        fmt.Println(strings.Join([]string{stand, "2"}, ""))
                    }
                }
            }
        }
    }
}

你这个是Go语言?

Reply View the author
hotime
deepin
2023-10-04 20:55
#8

你看看这个符合不?

package tech.hotime.practice;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 身份证类 (练习)
 *
 * @author hotime
 * @version 1.0
 * @date 2023/10/4
 */
public class IdentityCard {
    /**
     * 权重
     */
    private static final int[] WEIGHTS = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    /**
     * 校验码
     */
    private static final String[] CODES = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};

    public static void main(String[] args) {
        // 根据特定信息,生成可能存在的身份号码,并打印
        String[] areaCodes = {"610527", "610528", "610529"};
        List dateList = generateDateList(LocalDate.of(1991, 1, 1), LocalDate.of(1992, 12, 31));
        List idCardList = getIdCardList(areaCodes, dateList);
        System.out.println(idCardList);
        System.out.println("所有可能存在的身份号码一共有:" + idCardList.size() + "个,如上所述。");
    }

    /**
     * 生成自起始日期至结束日期之间的所有日期的字符串列表,格式为yyyyMMdd
     *
     * @param startDate 起始日期
     * @param endDate   结束日期
     * @return 所有日期的字符串列表
     */
    private static List generateDateList(LocalDate startDate, LocalDate endDate) {
        List dateList = new ArrayList<>();
        LocalDate currentDate = startDate;

        while (currentDate.isBefore(endDate) || currentDate.isEqual(endDate)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String dateStr = currentDate.format(formatter);
            dateList.add(dateStr);
            currentDate = currentDate.plusDays(1);
        }

        return dateList;
    }

    /**
     * 根据给定的部分地区码以及生辰日期列表,生成所有可能得身份证号码
     *
     * @param areaCodes 地区码 列表
     * @param dateList  生辰日期 列表
     * @return 身份证号码 列表
     */
    private static List getIdCardList(String[] areaCodes, List dateList) {
        ArrayList idCardList = new ArrayList<>();
        String idCard = "";
        for (String areaCode : areaCodes) {
            for (String date : dateList) {
                for (int i = 0; i < 999; i++) {
                    idCard = areaCode + date + String.format("%03d", i);
                    idCardList.add(idCard + getValidateCode(idCard));
                }
            }
        }
        return idCardList;
    }

    /**
     * 生成身份证第18位校验码 (采用ISO 7064:1983, MOD 11-2校验字符系统)
     *
     * @param idCard 身份证前17位
     * @return 身份证最后一位校验码
     */
    private static String getValidateCode(String idCard) {
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (idCard.charAt(i) - '0') * WEIGHTS[i];
        }
        return CODES[sum % 11];
    }
}
Reply View the author
199******64
deepin
2023-10-05 05:15
#9
hotime

你看看这个符合不?

package tech.hotime.practice;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * 身份证类 (练习)
 *
 * @author hotime
 * @version 1.0
 * @date 2023/10/4
 */
public class IdentityCard {
    /**
     * 权重
     */
    private static final int[] WEIGHTS = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    /**
     * 校验码
     */
    private static final String[] CODES = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};

    public static void main(String[] args) {
        // 根据特定信息,生成可能存在的身份号码,并打印
        String[] areaCodes = {"610527", "610528", "610529"};
        List dateList = generateDateList(LocalDate.of(1991, 1, 1), LocalDate.of(1992, 12, 31));
        List idCardList = getIdCardList(areaCodes, dateList);
        System.out.println(idCardList);
        System.out.println("所有可能存在的身份号码一共有:" + idCardList.size() + "个,如上所述。");
    }

    /**
     * 生成自起始日期至结束日期之间的所有日期的字符串列表,格式为yyyyMMdd
     *
     * @param startDate 起始日期
     * @param endDate   结束日期
     * @return 所有日期的字符串列表
     */
    private static List generateDateList(LocalDate startDate, LocalDate endDate) {
        List dateList = new ArrayList<>();
        LocalDate currentDate = startDate;

        while (currentDate.isBefore(endDate) || currentDate.isEqual(endDate)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
            String dateStr = currentDate.format(formatter);
            dateList.add(dateStr);
            currentDate = currentDate.plusDays(1);
        }

        return dateList;
    }

    /**
     * 根据给定的部分地区码以及生辰日期列表,生成所有可能得身份证号码
     *
     * @param areaCodes 地区码 列表
     * @param dateList  生辰日期 列表
     * @return 身份证号码 列表
     */
    private static List getIdCardList(String[] areaCodes, List dateList) {
        ArrayList idCardList = new ArrayList<>();
        String idCard = "";
        for (String areaCode : areaCodes) {
            for (String date : dateList) {
                for (int i = 0; i < 999; i++) {
                    idCard = areaCode + date + String.format("%03d", i);
                    idCardList.add(idCard + getValidateCode(idCard));
                }
            }
        }
        return idCardList;
    }

    /**
     * 生成身份证第18位校验码 (采用ISO 7064:1983, MOD 11-2校验字符系统)
     *
     * @param idCard 身份证前17位
     * @return 身份证最后一位校验码
     */
    private static String getValidateCode(String idCard) {
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (idCard.charAt(i) - '0') * WEIGHTS[i];
        }
        return CODES[sum % 11];
    }
}

"%03d" 请问这个是什么意思?我百度了一下,还是没有看懂

Reply View the author
hotime
deepin
2023-10-05 05:47
#10
199******64

"%03d" 请问这个是什么意思?我百度了一下,还是没有看懂

% 是格式符的开始。

d 表示有符号整数。

3 表示最小输出3位数字。

0 表示如果数字不足3位,在左边用0补成3位。

如 printf("%03d", 1); 会打印出 001


另外,你可以试着尝试使用一下AI工具,比如百度AI,直接把需求给他描述清楚,让他给你提供代码,然后审查它的代码,有哪里的代码不清楚什么意思直接问他,有疑惑直接问他,还是蛮方便的,平时能帮助解很多惑

Reply View the author
199******64
deepin
2023-10-05 07:10
#11
hotime

% 是格式符的开始。

d 表示有符号整数。

3 表示最小输出3位数字。

0 表示如果数字不足3位,在左边用0补成3位。

如 printf("%03d", 1); 会打印出 001


另外,你可以试着尝试使用一下AI工具,比如百度AI,直接把需求给他描述清楚,让他给你提供代码,然后审查它的代码,有哪里的代码不清楚什么意思直接问他,有疑惑直接问他,还是蛮方便的,平时能帮助解很多惑

学了一年多,累坏了!不想动了!

Reply View the author
199******64
deepin
2023-10-06 19:59
#12
hotime

% 是格式符的开始。

d 表示有符号整数。

3 表示最小输出3位数字。

0 表示如果数字不足3位,在左边用0补成3位。

如 printf("%03d", 1); 会打印出 001


另外,你可以试着尝试使用一下AI工具,比如百度AI,直接把需求给他描述清楚,让他给你提供代码,然后审查它的代码,有哪里的代码不清楚什么意思直接问他,有疑惑直接问他,还是蛮方便的,平时能帮助解很多惑

老板,咨询一下,idCard.charAt(i) - '0这段代码是什么意思?为何要-‘0’,反复也没有看懂

private static String getValidateCode(String idCard) {
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (idCard.charAt(i) - '0') * WEIGHTS[i];
        }
Reply View the author
hotime
deepin
2023-10-06 20:30
#13
199******64

老板,咨询一下,idCard.charAt(i) - '0这段代码是什么意思?为何要-‘0’,反复也没有看懂

private static String getValidateCode(String idCard) {
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            sum += (idCard.charAt(i) - '0') * WEIGHTS[i];
        }

减'0'是为了让每一位字符类型的数字变成int类型的数字,以便与权重相乘进行计算。

因为idCard是字符串,从它取出来的每一位都是ASCII码中“0-9”对应的字符,而不是int类型的数字,通过与字符'0'的相减,得到int类型的数值,正好是这个字符对应的数字本身。

比如字符'0'在ASCII码中是第48个字符,'1'是第49个字符,那么字符之间进行减法运算 '1' - '0',它会先转换为int型进行计算,相当于49 - 48,结果为1,类型int

Reply View the author
199******64
deepin
2023-10-06 22:01
#14
hotime

减'0'是为了让每一位字符类型的数字变成int类型的数字,以便与权重相乘进行计算。

因为idCard是字符串,从它取出来的每一位都是ASCII码中“0-9”对应的字符,而不是int类型的数字,通过与字符'0'的相减,得到int类型的数值,正好是这个字符对应的数字本身。

比如字符'0'在ASCII码中是第48个字符,'1'是第49个字符,那么字符之间进行减法运算 '1' - '0',它会先转换为int型进行计算,相当于49 - 48,结果为1,类型int

妙啊,谢谢铁子。

Reply View the author