当前位置:网站首页>生成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]);
}
}
}
边栏推荐
- Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
- 【OPENVX】对象基本使用之vx_reference
- Initializing, cleaning up and const decorating member functions of constructors
- mariadb相关说明
- 跳转语句break 和continue
- Implementation of website Icon
- [openvx] VX for basic use of objects_ graph
- 冰冰学习笔记:vim工具的基本操作
- Let's talk about the charm of code language
- Cases on classes and objects
猜你喜欢
随机推荐
bs4根据属性索引与名称索引对象
启牛是不是安全的证券公司啊开户安全吗?
Intel raid模拟器下载
华为再回应“清理34岁以上员工”传言,程序员如何应对“35岁危机”?
「疯狂食客」的元宇宙新布局「原始立方」,收藏价值几何?
jmeter面试话术
PKS的秘书&兄弟 | 温故知新
SOLIDWORKS CAM数据无法恢复,因为已检测到经化零件。
Redistemplate pipeline use
Let's talk about the charm of code language
【小程序的 插值表达式,渲染判断,绑定事件以及分享】
浅谈——网络安全架构设计(五)
坚持陪同学习
On the stability of common sorting
小程序商城如何精细化运营会员?
[openvx] VX for basic use of objects_ graph
顺序表和链表的优缺点及总结
做对的事情,把事情做对
这是一个笑话
Dark horse programmer - interface testing - four-day learning interface testing - the second day - Interface use case design, test points, function testing, security testing, performance testing, sing









