幸运数
package Remain;
public class Demo03 {
// 幸运数
// 员工卡号是四位整数[ 1000 , 9999 ] [1000,9999][1000,9999],各位数字的和就是参加抽奖活动的幸运数字,比如4512 45124512,4 + 5 + 1 + 2 = 12 4 + 5 + 1 + 2 = 124+5+1+2=12,幸运数字就是12 1212
// 输入员工卡号,编程计算该员工的幸运数字
public static void main(String[] args) {
// 方法一:直接拆分整数
int n = 5689;
int p1 = n % 10;//个位
int p2 = n / 10 % 10;
int p3 = n / 10 / 10 % 10;
int p4 = n / 10 / 10 / 10 % 10;
System.out.println(p3);
// 方法二:转换成字符串来处理
String str = String.valueOf(n);
int p5 = (int) str.charAt(0) - 48;
int p6 = (int) str.charAt(1) - 48;
int p7 = (int) str.charAt(2) - 48;
int p8 = (int) str.charAt(3) - 48;
System.out.println(p5+p6+p7+p8);
}
}