当前位置:网站首页>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点不见不散
一生朋友一生情,一生有你才会赢;千山万水总是情,点个关注行不行!

边栏推荐
- 登录模块调试-软件调试入门
- Test Management and Specification
- Paper reading (63): Get To The Point: Summarization with Pointer-Generator Networks
- OpenCV形状检测
- (1) Cloud computing technology learning - virtualized vSphere learning
- DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
- How does the new retail saas applet explore the way to break the digital store?
- DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计
- 每日练习------生成13位条形, Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
- 完美绕开CRC32检测的无痕hook
猜你喜欢

华为云数据治理生产线DataArts,让“数据‘慧’说话”

Visual Studio编辑器 2019:scanf函数返回值被忽略(C4996)报错及解决办法

代码越写越乱?那是因为你没用责任链

Public Key Retrieval is not allowed报错解决方案

Paper reading (63): Get To The Point: Summarization with Pointer-Generator Networks

从零开始的Multi-armed Bandit

arcpy tutorial

HUAWEI CLOUD data governance production line DataArts, let "data 'wisdom' speak"

MySql统计函数COUNT详解

.NET 6.0中使用Identity框架实现JWT身份认证与授权
随机推荐
真正懂经营管理的CIO具备哪些特质
如何写一份高可读性的软件工程设计文档
华为云数据治理生产线DataArts,让“数据‘慧’说话”
What does a good resume look like in the eyes of a big factory interviewer?
初识二叉搜索树
MySQL 8.0.29 解压版安装教程(亲测有效)
你是一流的输家,你因此成为一流的赢家
Express框架连接MySQL及ORM框架
DTSE Tech Talk丨Phase 2: 1 hour in-depth interpretation of SaaS application system design
牛客网刷题——运算符问题
优酷视频元素内容召回系统:多级多模态引擎探索
lotus 爆块失败
数组和指针(2)
Go新项目-编译热加载使用和对比,让开发更自由(3)
onenote use
Discuz magazine/news report template (jeavi_line) UTF8-GBK template
You are a first-class loser, you become a first-class winner
数据的存储
[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction
DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计