基础Java练习08:由卡号计算幸运数字

  • 员工卡号是员工四位数字,各个数字之和就是抽奖活动的幸运数字。
  • 输入员工卡号,计算该员工的幸运数字。

编写程序

/**
 * 功能:由卡计算幸运数字
 * 作者:孤梦
 * 日期:2022年03月24日
 */
public class Example04 {
    
    
    public static void main(String[] args) {
    
    
        int id,  p1, p2,p3, p4,x;
        Scanner sc =  new Scanner(System.in);
        System.out.print("请输入卡号id: ");
        id = sc.nextInt();
        p1 = id / 1000; // 千位
        p2 = (id / 100) % 10; // 百位
        p3 = (id / 10) % 10; // 十位
        p4 = id % 10; // 个位
        x = p1+p2+p3+p4;
        System.out.println("x = " + x);
        System.out.println("恭喜该员工的幸运数字是:" + x);
    }
}

运行程序,查看结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_62491692/article/details/123717662