个位数,十位数,百位数等程序常用英文变量名该怎么写?
2016-07-24 15:14
501 查看
学习Java时,国内老师往往使用gw, sw, bw等汉语拼音缩写来代替,但是身为事事追求尽善尽美和professional范的准工程师们,我们怎么能满足于这么low的写法呢!
首先我上网查询了下个位数,十位数等英文的表达,得到的答案如下:
In the number 386 for example, the number 6 is the "unit's digit" (in the "unit's place"), 8 is the "ten's digit"
(in the "ten's place"), and 3 is the "hundred's digit."
所以呢,我们可以知道,标准的说法就是:
个位 unit's digit;
十位 ten's digit;
百位 hundred's digit;
千位 thousand's digit;
万位 ten thousand's digit;
但是这样让我们怎么起变量名啊,变量名里面我们是不能使用单引号( ' )的。回忆下,老师说了,Java里面只能用大小写字母,数字,下划线和$,而且开头不能为数字。
因此,一个比较折中,而且简洁的办法是说,我们可以按照从右到左第几位数来给变量命名,这样不仅直白,而且在有类似身份证号,银行卡号等十几位数字的时候新变量命名的可扩展性很好。(写程序一定要有容错性和可扩展性)
个位 dig_1;
十位 dig_2;
百位 dig_3;
千位 dig_4;
万位 dig_5;
代码示范
首先我上网查询了下个位数,十位数等英文的表达,得到的答案如下:
In the number 386 for example, the number 6 is the "unit's digit" (in the "unit's place"), 8 is the "ten's digit"
(in the "ten's place"), and 3 is the "hundred's digit."
所以呢,我们可以知道,标准的说法就是:
个位 unit's digit;
十位 ten's digit;
百位 hundred's digit;
千位 thousand's digit;
万位 ten thousand's digit;
但是这样让我们怎么起变量名啊,变量名里面我们是不能使用单引号( ' )的。回忆下,老师说了,Java里面只能用大小写字母,数字,下划线和$,而且开头不能为数字。
因此,一个比较折中,而且简洁的办法是说,我们可以按照从右到左第几位数来给变量命名,这样不仅直白,而且在有类似身份证号,银行卡号等十几位数字的时候新变量命名的可扩展性很好。(写程序一定要有容错性和可扩展性)
个位 dig_1;
十位 dig_2;
百位 dig_3;
千位 dig_4;
万位 dig_5;
代码示范
/* * Six-digit input and calculate their sum; * 2016-07-24-Sunday */ import java.util.Scanner; public class Task_01 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Input a six-digit card number: "); int num=input.nextInt(); int Dig_1=num%10; int Dig_2=num/10%10; int Dig_3=num/100%10; int Dig_4=num/1000%10; int Dig_5=num/10000%10; int Dig_6=num/100000%10; int sum=Dig_1+Dig_2+Dig_3+Dig_4+Dig_5+Dig_6; System.out.println("The sum is "+sum); } }
相关文章推荐
- 常用排序算法之希尔排序
- 编译ITK
- DataTables 自定义
- java 命令notes
- CentOS7 升级python同时解决yum损坏问题
- 简单dp算法——Sumsets
- poj 2524 Ubiquitous Religions [并查集]
- 常用排序算法之插入排序
- Glut处理鼠标事件
- java换行操作
- 笔试题中的必胜策略
- JDBC核心
- 希尔排序和归并排序(java实现)
- 几款开源图像处理软件评测研究
- 如何在静态博客hexo中只显示摘要信息
- mysql数据库字段操作
- [译]接口隔离原则在Android中的实践
- 常用排序算法之快速排序
- Android studio使用HttpClient出错
- Swift编程基础之数组去重 + 模拟双色球