当前位置:网站首页>生成13位条形码
生成13位条形码
2022-07-22 23:51:00 【千寻~~】
13位条形码规则:第十三位数字是前十二位数字经过计算得到的校验码。
例如:690123456789(前12为随机生成,得出第13位)
计算其校验码的过程为:
@前十二位的奇数位和6+0+2+4+6+8=26
@前十二位的偶数位和9+1+3+5+7+9=34
@将奇数和与偶数和的三倍相加26+34*3=128
@取结果的个位数:128的个位数为8
@用10减去这个个位数10-8=2
所以校验码为2
(注:如果取结果的个位数为0,那么校验码不是为10(10-0=10),而是0)
实现方法ean13()计算验证码,输入12位条码,返回带验证码的条码。
例:输入:692223361219输出:6922233612192
基本思路:
随机生成12个数字,以此储到数组中。依据题目所给出的要求求出第13位,把得出的第13位校验码存储到数组的最后一位。再打印出数组。
代码:
import java.util.Random;
public class Exer {
public static void main(String[] args) {
Random random = new Random();
int[] arr = new int[13];//定义一个长度为13的数组
int oddSum = 0;//奇数位和
int evenSum = 0;//偶数位和
System.out.print("输入的12位条形码为:");
for(int i = 0; i < 12; i++) {
arr[i] = random.nextInt(10);//把随机生成的数存放到数组中
System.out.print(arr[i]);
if(i % 2 == 0) {
oddSum += arr[i];
}else {
evenSum += arr[i];
}
}
System.out.println();
int sum = oddSum + evenSum * 3;//奇数和与偶数和的三倍相加
int ge = sum % 10;//奇数和与偶数和的三倍相加结果的个位数
System.out.print("返回带验证码的条码为:");
for(int i = 0; i < arr.length; i++) {
if(ge == 0) {
arr[arr.length] = 0;
}else {
arr[arr.length-1] = 10 - ge;
}
System.out.print(arr[i]);
}
}
}
边栏推荐
- UE5分屏(小地图)的解决方案
- Implementation of website Icon
- 数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
- 实现 FinClip 微信授权登录的三种方案
- Container monitoring three swordsman cadvisor collects monitoring data + influxdb stores data + granfana shows an introduction to chart data
- 笔试强训第21天
- SolidWorks无法引用参考
- 用户登录程序C语言实现
- 浅谈——网路安全架构设计(一)
- 做对的事情,把事情做对
猜你喜欢
随机推荐
Talking about -- network security architecture design (4)
这是一个笑话
Node definition and traversal of binary tree~
Since I used the hiflow scene connector, I don't have to worry about becoming a "drowned chicken" anymore
测试/开发程序员如何突破?条条大路通罗马......
【Unity项目实践】FSM有限状态机
C语言编写“Hello World”挑战赛,你会如何作答?
Add payment method after successful registration of Alibaba cloud international edition
Arcgis js api二次开发——加载国家天地图
HCIP - - - BGP综合实验
1.5万字概括ES6全部特性
测试面试题
工控人,你真的了解你的五险一金吗?
XMODEM, ymodem and zmodem protocols are the three most commonly used communication protocols
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
你知道怎么做好接口测试?
聊聊队列(FIFO)的应用
动态规划及马尔可夫特性最佳调度策略(Matlab完整代码实现)
国债逆回购安全吗 如何网上开户?
UE5分屏(小地图)的解决方案









