用java程序验证身份证男女

发布网友

我来回答

4个回答

热心网友

public static void main(String[] args) {
String id ="510111199212018271";

String birth = id.substring(6, 14);

String sex = id.substring(16, 17);
if(Integer.parseInt(sex)%2==0){
sex = "女";
}else{
sex ="男";

}
System.err.println("生日:"+birth+"性别"+sex);
}

热心网友

第一题:

import java.util.Arrays;
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner sc=null;
//存放质数的数组
int[] arr=new int[10];
a:for(int i=0;i<1;i++){
System.out.print("请输入第"+(i+1)+"个数: ");
sc=new Scanner(System.in);
String str=sc.next();
try {
int temp=Integer.parseInt(str);
if(temp>=2){
for(int j=2;j<temp;j++){
if(temp%j==0){
break a;
}
}
arr[i]=temp;
}

} catch (Exception e) {
System.out.println("输入不为数字,程序退出");
System.exit(0);
}
}

Arrays.sort(arr);
if(arr[9]!=0){
System.out.println("最大质数为:"+arr[9]);
}else{
System.out.println("没有质数");
}
}
}


第二道身份证的:

public class Test {
public static void main(String[] args) {
//String cardId="130503670401001";
String cardId="510111199212018271";
if(cardId.length()==15){
System.out.println("生日:"+cardId.substring(8, 10)+"月"+cardId.substring(10, 12)+"日");
System.out.println("性别:"+((Integer.parseInt(cardId.substring(14))%2==0?"女":"男")));
}else if(cardId.length()==18){
System.out.println("生日:"+cardId.substring(10, 12)+"月"+cardId.substring(12, 14)+"日");
System.out.println("性别:"+((Integer.parseInt(cardId.substring(16))%2==0?"女":"男")));
}else{
System.out.println("身份证位数不正确");
}
}
}

热心网友

1 Scanner scan = new Scanner(System.in);
int a[] = new a[10];
int maxjs = 0;

for(int i=0;i<10;i++){
a[i] = scan.read();
if(a[i]/2==0||a[i]>maxjs){
maxjs = a[i];

}

}
if(maxjs==0){
System.out.println("没有奇数
");

}else{
System.out.println(maxjs);
}

2 String zjhm = "510111199212018271";
if(zjhm.length!=18){
System.out.println("error! ");
return ;
}
String birthday = zjhm .substring(10,14);
char sex = zjhm .charAt(str.length-2);
System.out.println("birthday:"+birthday);
if(sex/2==0){
System.out.println("girl");
}else{
System.out.println("boy");
}

热心网友

1、

public static void main(String[] args) {
// TODO Auto-generated method stub
int a[] = {0,2,32,4141,54,6,78,81,96,10};//任意十个整数
int b;
int c[] = new int[10];
int j = 0;
int maxJ;
boolean flag = false;
for (int i=0;i<a.length;i++){
//判断是否奇数
b = a[i] % 2;
if (b == 1){
flag = true;
c[j] = a[i];
j++;
}
}
//求最大奇数
if (flag){
maxJ = c[0];
for (int k=0;k<c.length;k++)
{
if (c[k] > maxJ)
{
maxJ = c[k];
}
}
System.out.println(maxJ);
return;
}
System.out.println("没有奇数");
}

2、

    /**
     * 通过身份证号号获取生日日期
     * @param IdNo String
     * @return String
     */
    public static String getBirthdayFromId(String IdNo)
    {
        String tIdNo = StrTool.cTrim(IdNo);
        String birthday = "";
        if (tIdNo.length() != 15 && tIdNo.length() != 18)
        {
            return "";
        }
        if (tIdNo.length() == 18)
        {
            birthday = tIdNo.substring(6, 14);
            birthday = birthday.substring(0, 4) + "-" + birthday.substring(4, 6) +
                       "-" + birthday.substring(6);
        }
        if (tIdNo.length() == 15)
        {
            birthday = tIdNo.substring(6, 12);
            birthday = birthday.substring(0, 2) + "-" + birthday.substring(2, 4) +
                       "-" + birthday.substring(4);
            birthday = "19" + birthday;
        }
        return birthday;

    }

    /**
     * 通过身份证号获取性别
     * @param IdNo String
     * @return String 0-男 1-女
     */
    public static String getSexFromId(String IdNo)
    {
        String tIdNo = StrTool.cTrim(IdNo);
        if (tIdNo.length() != 15 && tIdNo.length() != 18)
        {
            return "";
        }
        String sex = "";
        if (tIdNo.length() == 15)
        {
            sex = tIdNo.substring(14, 15);
        }
        else
        {
            sex = tIdNo.substring(16, 17);
        }
        try
        {
            int iSex = Integer.parseInt(sex);
//            iSex = iSex % 2;
            iSex %= 2;
            if (iSex == 0)
            {
                return "1";
            }
            if (iSex == 1)
            {
                return "0";
            }
        }
        catch (Exception ex)
        {
            return "";
        }
        return "";
    }

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com