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

边栏推荐
- LeetCode318:单词长度的最大乘积
- 阿里巴巴中国站获得1688商品分类 API
- Navisworks切换语言
- HUAWEI CLOUD data governance production line DataArts, let "data 'wisdom' speak"
- 归一化与标准化
- Wuhan Star Sets Sail: Overseas warehouse infrastructure has become a major tool for cross-border e-commerce companies to go overseas
- SocialFi 何以成就 Web3 去中心化社交未来
- [MRCTF2020]Ezaudit
- (一)云计算技术学习--虚拟化vSphere学习
- The service already exists! Solution
猜你喜欢

What does a good resume look like in the eyes of a big factory interviewer?

说几个大厂分库分表的那点破事。

Scheduling_Channel_Access_Based_on_Target_Wake_Time_Mechanism_in_802.11ax_WLANs

数据库的三大范式

DTSE Tech Talk丨第2期:1小时深度解读SaaS应用系统设计

Gorilla Mux 和 GORM 的使用方法

第一次用debug查询,发现这个为空,是不是代表还没获得数据库的意思?求帮助。

有没有并发系统设计的经验,我该怎么说?

Go新项目-编译热加载使用和对比,让开发更自由(3)
![[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction](/img/29/92b9d52d17a203b8bdead3eb2c902e.png)
[NCTF2019] Fake XML cookbook-1|XXE vulnerability|XXE information introduction
随机推荐
PHP留言反馈管理系统源码
京东获取推荐商品列表 API
每日练习------生成13位条形, Ean-13码规则:第十三位数字是前十二位数字经过计算得到的校验码。
升级Win11后不喜欢怎么退回Win10系统?
MySQL详细学习教程(建议收藏)
[NCTF2019]Fake XML cookbook-1|XXE漏洞|XXE信息介绍
3D激光SLAM:LeGO-LOAM论文解读---系统概述部分
MySQL 8.0.29 解压版安装教程(亲测有效)
[TypeScript]简介、开发环境搭建、基本类型
李沐d2l(七)kaggle房价预测+数值稳定性+模型初始化和激活函数
SwiftUI SQLite教程之带有历史的搜索栏List App (教程含完整代码)
云厂商做生态需要“真连接、真赋能”,用“技术+真金实银”发展伙伴
[HarekazeCTF2019]Avatar Uploader 1
Minio 入门
The first time I used debug query and found that this was empty, does it mean that the database has not been obtained yet?please help.
OpenCV形状检测
CMake库搜索函数居然不搜索LD_LIBRARY_PATH
You are a first-class loser, you become a first-class winner
Test Management and Specification
【Linux Operating System】 Virtual File System | File Cache