当前位置:网站首页>Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
2022-07-30 16:54:00 【Bei Ning Mo language】
题目:
生成13位条形码
Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码.
例如:690123456789
计算其校验码的过程为:
@前十二位的奇数位和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
解题关键:12Each number requires a separate output,Then make a separate judgment,Finally, the check number is judged
思路:1)依次输入12位数字
2)创建一个longType to contain the entered number
3)Add the entered numberslong类型
4)判断输入的数字
5)输出12位数字
6)判断第13bit check number
7)The thirteenth digit was addedlong类型中
过程: 接下来我们根据我们的解题思路来一步步写代码
1)依次输入12位数字
Scanner sc = new Scanner(System.in);
System.out.println("请您依次12位条码:");
int sumOuShuWei = 0;
int sumJiShuWei= 0;
2)创建一个longType to contain the entered number
long all12 = 0L;
for(int i = 1; i < 13; i++){
System.out.println("请您输入第" + i + "位数字");
int num = sc.nextInt();
3)Add the entered numberslong类型
all12 = all12 * 10l + (long)num;
4)判断输入的数字
if(num<0||num>9){
System.out.println("请输入0~9直接的整数");
}
if(num %2 == 0){
sumJiShuWei+= num;
}else{
sumOuShuWei += num;
}
}
5)输出12位数字
System.out.println("12数位:" + all12);
6)判断第13bit check number
int sumJiOu = sumJiShuWei+sumOuShuWei*3;
int geWei = sumJiOu % 10;
int jiaoYanMa = 0;
if(geWei != 0){
jiaoYanMa = 10 -geWei;
}
7)The thirteenth digit was addedlong类型中
all12 = all12*10 + jiaoYanMa;
System.out.println("13位数位:" + all12);
完整结果如下:

为了方便大家使用,下面附上源码:
//1)依次输入12位数字
Scanner sc = new Scanner(System.in);
System.out.println("请您依次12位条码:");
int sumOuShuWei = 0;
int sumJiShuWei= 0;
//2)创建一个longType to contain the entered number
long all12 = 0L;
for(int i = 1; i < 13; i++){
System.out.println("请您输入第" + i + "位数字");
int num = sc.nextInt();
//3)Add the entered numberslong类型
all12 = all12 * 10l + (long)num;
//4)判断输入的数字
if(num<0||num>9){
System.out.println("请输入0~9直接的整数");
}
if(num %2 == 0){
sumJiShuWei+= num;
}else{
sumOuShuWei += num;
}
}
//5)输出12位数字
System.out.println("12数位:" + all12);
//6)判断第13bit check number
int sumJiOu = sumJiShuWei+sumOuShuWei*3;
int geWei = sumJiOu % 10;
int jiaoYanMa = 0;
if(geWei != 0){
jiaoYanMa = 10 -geWei;
}
//7)The thirteenth digit was addedlong类型中
all12 = all12*10 + jiaoYanMa;
System.out.println("13位数位:" + all12);总结:
The key to this question is to enter in order12个数字,Of course there must be an easier way to write this question,My solution to this problem uses onelongtype to accept data,This question is written with an embedded loop.
明日练习:随机产生一个1-100之间的整数,看能几次猜中.要求:猜的次数不能超过7次,每次猜完之后都要提示“大了”或者“小了”.
大家可以自己写写,明天中午12点我准时发出我的写法哦,明天12点不见不散
一生朋友一生情,一生有你才会赢;千山万水总是情,点个关注行不行!

边栏推荐
- 报错500,“message“: “nested exception is org.apache.ibatis.binding.BindingException: 解决记录
- What does a good resume look like in the eyes of a big factory interviewer?
- 你是这样的volatile,出乎意料
- 有没有并发系统设计的经验,我该怎么说?
- 对话框 QDialog ( 详解 )
- vivo announced to extend the product warranty period, the system launched a variety of functional services
- 23. 请你谈谈关于IO同步、异步、阻塞、非阻塞的区别
- CMake库搜索函数居然不搜索LD_LIBRARY_PATH
- 数据库的三大范式
- PHP留言反馈管理系统源码
猜你喜欢
随机推荐
优酷视频元素内容召回系统:多级多模态引擎探索
3D激光SLAM:LeGO-LOAM论文解读---特征提取部分
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
理解实现搜索二叉树
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
数据库课程设计大作业大盘点【建议在校生收藏】
lotus 1.16.0 最小快照导出 导入
初识二叉搜索树
详解最实用的几种dll注入方式
DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design
华为云数据治理生产线DataArts,让“数据'慧'说话”
Mysql进阶优化篇01——四万字详解数据库性能分析工具(深入、全面、详细,收藏备用)
Chapter 6: Decisive Autumn Moves
Express框架连接MySQL及ORM框架
onenote use
The service already exists! Solution
疫情之下的裁员浪潮,7点建议帮你斩获心仪offer
服务器装好系统的电脑怎么分区
3D激光SLAM:LeGO-LOAM论文解读---实验对比
华为云数据治理生产线DataArts,让“数据‘慧’说话”
![[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction](/img/29/92b9d52d17a203b8bdead3eb2c902e.png)








