您的位置:首页 > 其它

个位数,十位数,百位数等程序常用英文变量名该怎么写?

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;

代码示范

/*
* 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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: