javaSE基础例题-输入出生年月日计算年龄

文章介绍了两个Java程序片段,分别用于计算用户输入的出生日期后得到当前年龄,并在某些条件下判断是否为生日。第一个方法逐月逐日检查,第二个方法直接比较出生日期和当前日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//输入出生年月日得出年龄
public static void test3() {
        int a, b, c;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的出生年份");
        int x = sc.nextInt();
        System.out.println("请输入你的出生月份");
        int y = sc.nextInt();
        System.out.println("请输入你的出生日");
        int z = sc.nextInt();
        a = 2023 - x;
        b = 7 - y;
        c = 30 - z;
        if (a > 0) {
            if (b > 0 && c > 0) {
                System.out.println("你今年" + a + "岁了");
            } else if (b < 0) {
                a--;
                System.out.println("你今年" + a + "岁了");
            } else if (b > 0 && c < 0) {
                a--;
                System.out.println("你今年" + a + "岁了");
            } else if (b ==0 &&c>0) {
                System.out.println("你今年" + a + "岁了");
            }else if (b == 0&&c<0){
                a--;
                System.out.println("你今年" + a + "岁了");
            }else if (b ==0 &&c ==0){
                System.out.println("HAPPY BIRTHDAY!");
            }
        } else if (a < 0) {
            System.out.println("吃奶去");
        }


    }

//算年龄的第二种方法
public static void shengri(){
        Scanner input =new Scanner(System.in);
        System.out.println("出生日期");
        int a1 = input.nextInt();
        int b1 =input.nextInt();
        int c1 =input.nextInt();
        Scanner input1 =new Scanner(System.in);
        System.out.println("当前日期");
        int a2 = input1.nextInt();
        int b2 = input1.nextInt();
        int c2 = input1.nextInt();
        int x1,y1,x2,y2,c;
        x1 = b1 * 100 + c1;
        y1=a1;

        x2 = b2 * 100 + c2;
        y2=a2;

        if( x1 >= x2 ){
            c = y2 - y1 - 1 ;
            System.out.println(c);
        }
        else  if( x1 < x2 ){
            c = y2 - y1 ;
            System.out.println(c);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值