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

学习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);
}
}



  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值