判断条形码校验码的正确性Java代码

/**
* 判断条形码校验码的正确性
* 条形码格式:12位数+1位校验码;如9771671216014
* 判断方法:把条形码把所有偶数序号位上的数相加求和,
* 用求出的和乘3,再把所有奇数序号上的数相加求和,
* 用求出的和加上刚才偶数序号上的数,然后得出和。
* 再用10减去这个和的个位数,就得出校验码,
* 如果得10则为校验码为0
* @author ----
*
*/
public class Barcode {
public static void main(String[] args) {
String str = "6921317905038";
int key = checkCode(two(str),one(str));
if(key == (str.charAt(str.length()-1)-'0')){
System.out.println("校验码正确");
}else{
System.out.println("校验码错误,正确的校验码是"+key);
}
}
public static int two(String str){//偶位数的和*3
int m = 0;
for(int i= 1;im += (str.charAt(i)-'0');
}
return m*3;
}
public static int one(String str){//奇位数的和
int n = 0;
for(int i= 0;in += (str.charAt(i)-'0');
}
return n;
}
public static int checkCode(int m,int n){//校验码理论值
int c = 0;
if(((m+n)%10)==0){
c = 0;
}else{
c = 10-((m+n)%10);
}
return c;
}
}

相关文档
最新文档