求Java大神给个代码!计算验证条形码

2024-12-03 19:06:56
推荐回答(1个)
回答1:

public class Ean13Barcode {

private String code;

public Ean13Barcode(String code) {
super();
this.code = code;
}

public String encode() {
if (null == code) {
return "";
}
char[] codes = code.toCharArray();
int sum = 0;
for (int i = 0; i < codes.length; i++) {
int num = codes[i] - '0';
if (isEven(num)) {
sum += num;
} else {
sum += num * 3;
}
}
int x = sum % 10;
return code + (x == 0 ? 0 : 10 - x);
}

private boolean isEven(int x) {
return x % 2 == 0;
}

public static void main(String[] args) {
System.out.println(new Ean13Barcode("692223361219").encode());
}

}