计算自己活了多少岁

public static void timu13(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入年:");
        int shuYear=scanner.nextInt();
        System.out.println("请输入月:");
        int shuMonth=scanner.nextInt();
        System.out.println("请输入日:");
        int shuDay=scanner.nextInt();

        //获取当前年份,月份,日
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);

        //计算出多少年到多少年的总天数
        int day1=yearDays(shuYear, year);   
        //计算出出生那年已经过了多少天
        int day2=passedDays(shuYear, shuMonth, shuDay); 
        //计算出现在这年已经过了多少天
        int day3=passedDays(year, month, day);

        System.out.println(day1-day2+day3);
    }
    //计算出多少年到多少年的总天数
    public static int  yearDays(int beginYear,int endYear){
        int days=0;
        for(int i=beginYear;i366:365;
        }
        return days;
    }


    //计算出这一年已经过了多少天
    public static int passedDays(int year,int month,int day){
        int days=0;
        for(int i=1;iswitch (i) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days+=31;
                break;
            case 2:
                days+=isRun(year)?29:28;
                break;          
            default:
                days+=30;
                break;
            }
        }
        System.out.println("这"+year+"年已经过了"+(days+day)+"天");
        return days+day;
    } 
    // 判断是否为闰年
    public static boolean isRun(int year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }

你可能感兴趣的:(清茶)