~这些年,我们一起学过的java~13~小学期程序设计之身份证校验

       嘿嘿,昨晚把身份证校验的问题解决了,最最开始卡在了如何将JTextfield的gettext()方法中获取到的字符串变成单个单个的数字进行数据计算,后来雄哥就教我用charAt方法得到一个一个的char类型,但是变成int型之后代表的是相对应的ASCII码值,之后就是简单的数据计算吧,木有太难的地方……

 

题目在此:

题目描述

我国国标〖GB 11643-1999〗中规定:公民身份号码是18位特征组合码,由十七位数字本体码和一位数字校验码组成。排列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺序码和一位数字校验码。其校验码(最后一位)计算方法和步骤为:

(1)十七位数字本体码加权求和公式

S = Sum(Ai * Wi), i = 0, ... , 16,先对前17位数字的权求和

其中Ai:表示第i位置上的身份证号码数字值

Wi:表示第i位置上的加权因子,前17位加权因子从左到右分别为

Wi7 9 10 5 8 42 1 6 3 7 9 10 5 8 4 2

(2)计算模

Y = mod(S, 11)

(3)通过模Y查下表得到对应的校验码

Y

0

1

2

3

4

5

6

7

8

9

10

校验码

1

0

X

9

8

7

6

5

4

3

2

例如:某身份证前17位为11010519491231002

i

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

wi

7

9

10

5

8

4

2

1

6

3

7

9

10

5

8

4

2

 

1

1

0

1

0

5

1

9

4

9

1

2

3

1

0

0

2

7

9

0

5

0

20

2

9

24

27

7

18

30

5

0

0

4

得到和为:167;则模为y=167%11=2

(3)得校验码为x

请按上面所述步骤编程,输入一个二代身份证号,检查该身份证是否正确。

 

 

下面是我的代码的运行结果:

 

 

附上俺滴代码:

import java.awt.Color;
import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class IDcheck extends JFrame {
	MyListener l;
	public static void main(String[] args) {
		IDcheck ic=new IDcheck();
		ic.GUI();
	}
	public void GUI(){
		this.setTitle("身份证校验");
		this.setSize(650, 600);
		this.setLocation(350, 40);
		this.setDefaultCloseOperation(3);
		this.setResizable(false);
		this.setLayout(null);
		
		ImageIcon im5=new ImageIcon(this.getClass().getResource("5.jpg"));
		this.setIconImage(im5.getImage());
		
		JPanel jp1=new JPanel();
		jp1.setLayout(null);
		jp1.setBounds(0, 0, 650, 150);
		jp1.setBackground(Color.white);
		
		ImageIcon im1 =new ImageIcon(this.getClass().getResource("2.PNG"));
		JLabel jl1=new JLabel(im1);
		jl1.setBounds(10, 9, im1.getIconWidth(), im1.getIconHeight());
		jp1.add(jl1);
	
		JPanel jp2=new JPanel();
		jp2.setBounds(0, 150,300,450);
		jp2.setLayout(null);

		
		ImageIcon im2=new ImageIcon(this.getClass().getResource("4.png"));
		JLabel jl2=new JLabel(im2);
		jl2.setBounds(15,20, im2.getIconWidth(), im2.getIconHeight());
		jp2.add(jl2);
		
		JPanel jp3=new JPanel();
		jp3.setLayout(null);
		jp3.setBounds(300, 150, 350, 450);
		
		JLabel jl3=new JLabel("请输入待测身份证号码:");
		jl3.setBounds(20, 40, 350, 40);
		jl3.setFont(new Font("楷体",2, 24));
		
		JTextField jt=new JTextField();
		jt.setBounds(30, 110, 225, 35);
		jt.setHorizontalAlignment((int)CENTER_ALIGNMENT);
		jt.setFont(new Font("楷体", 2, 20));
		
		JButton jb=new JButton("检验号码");
		jb.setBounds(65, 180, 150, 45);
		jb.setFont(new Font("楷体", 3, 24));
		
		JLabel jl4=new JLabel();
		jl4.setBounds(0, 250, 350, 110);
		
		jp3.add(jl4);
		jp3.add(jb);
		jp3.add(jl3);
		jp3.add(jt);
		
		l=new MyListener(jt,jl4);
		jb.addActionListener(l);
		
		this.add(jp3);
		this.add(jp2);
		this.add(jp1);
		this.setVisible(true);
	}
}

 

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class MyListener implements ActionListener{
	
	JTextField jt;
	JLabel jl4;
	ImageIcon im7=new ImageIcon(this.getClass().getResource("7.PNG"));
	ImageIcon im5=new ImageIcon(this.getClass().getResource("5.PNG"));
	ImageIcon im8=new ImageIcon(this.getClass().getResource("8.png"));
	public MyListener(JTextField jt,JLabel jl4){
		this.jt=jt;
		this.jl4=jl4;
		
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if("检验号码".equals(e.getActionCommand())){
			String str=jt.getText();
			if(str.length()!=18){
				jl4.setIcon(im8);
			}
			
			int array[]=new int[str.length()];
			int s=0,y=0;
		    char chk[]={1,0,'X',9,8,7,6,5,4,3,2};
			int check[]={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
			for(int i=0;i<17;i++){
				array[i]=str.charAt(i)-48;
				s=s+check[i]*array[i];
//				System.out.println(array[i]+"***********");
			}
//			array[17]=str.charAt(17)-48;
			y=s%11;
			if(y==0){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==1){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==2){
				if(chk[y]==str.charAt(17)){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==3){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==4){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==5){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==6){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==7){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==8){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==9){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			if(y==10){
				if(chk[y]==str.charAt(17)-48){
					jl4.setIcon(im7);
				}else{
					jl4.setIcon(im5);
				}
				
			}
			
		}
	}
	

}



       现在编程的速度越来越快了,敲代码也越来越欢快了,好开森哈,慢慢的一步一步地前进,终究会走到自己期待的那个地方……

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值