~這些年,我們一起學過的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);
				}
				
			}
			
		}
	}
	

}



       現在編程的速度越來越快了,敲代碼也越來越歡快了,好開森哈,慢慢的一步一步地前進,終究會走到自己期待的那個地方……

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章