[Java] 纯文本查看 复制代码 package 身份证验证小程序;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入18位身份证:");
while(true)
{
String str = sc.nextLine();
char[] arr =str.toCharArray();
if(panduan(arr))
{
System.out.println("输入正确!");
break ;
}
System.out.println("输入错误,请重新输入:");
}
}
public static boolean panduan(char[] arr)
{
int weight[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
char map[] = {'1','0','X','9','8','7','6','5','4','3','2'};
int sum = 0 ;
for(int i = 0 ; i < 17 ; i++)
{
sum += (arr[i]-'0') * weight[i];
}
int i = sum%11 ;
char c = arr[arr.length-1];
if(map[i] == c)
{
return true ;
}
return false ;
}
}
|