当前位置:网站首页>Rules for generating 13 digit barcode EAN-13 Code: the thirteenth digit is the verification code obtained by calculating the first twelve digits.

Rules for generating 13 digit barcode EAN-13 Code: the thirteenth digit is the verification code obtained by calculating the first twelve digits.

2022-07-24 02:55:00 Quarter seventeen

subject

         

Generate 13 Bit barcode

Ean-13 Code rule : The thirteenth digit is the check code calculated from the first twelve digits .

for example :690123456789

The process of calculating its check code is :

@ The odd sum of the first twelve digits 6+0+2+4+6+8=26

@ The sum of the even digits of the first twelve 9+1+3+5+7+9=34

@ Add three times the sum of odd and even numbers 26+34*3=128

@ Take the single digit of the result :128 The single digit of is 8

@ use 10 Subtract this single digit 10-8=2

So the check code is 2

( notes : If the single digit of the result is 0, Then the check code is not 10(10-0=10), It is 0)

Implementation method ean13() Calculate the verification code , Input 12 Bit bar code , Return the barcode with verification code .

example : Input :692223361219 Output :6922233612192

Code

             

import java.util.Scanner;
public class Demo00 {
	public static void main(String[] args) {
		System.out.println(" Please enter 12 digits as barcode ");
		Scanner sc = new Scanner(System.in);
		long number = sc.nextLong();
		long number2 = number;
		long[] array = new long[12];
		for (int i = 0; i < 12; i++) {
			array[i] = number % 10;
			number /= 10;
		}
		int oddSum = 0;//  Odd number 
		int evenSum = 0;//  even numbers 

		//  Odd and 
		for (int i = 0; i < 12; i = i + 2) {
			oddSum += array[i];
		}
		//  Even and 
		for (int j = 1; j < 12; j = j + 2) {
			evenSum += array[j];
		}
		int threeSum = oddSum * 3 + evenSum;
		int geWei = threeSum % 10;
		int jiaoYan = 0;
		if (geWei != 0)// Judge whether it is 0  Not by 10 reduce   It's the same 
			jiaoYan = 10 - geWei;
		long number3 = number2 * 10 + jiaoYan;
		System.out.println(number3);
	}
}

原网站

版权声明
本文为[Quarter seventeen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207222351350887.html