获取星座和属相

获取星座


 /**
     * 获取星座
     *
     * @param month 生日 月
     * @param day   生日 日
     * @return 返回星座
     */
    private String getHoroscopes(int month, int day) {
        String[] astro = new String[]{"摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座",
                "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座"};
        int[] arr = new int[]{20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22};// 两个星座分割日
        int index = month;
        // 所查询日期在分割日之前,索引-1,否则不变
        if (day < arr[month - 1]) {
            index = index - 1;
        }
        // 返回索引指向的星座string
        return astro[index];
    }

获取属相

    /**
     * 根据年获取属相
     *
     * @param year 生日 年
     * @return 返回属相
     */
    private String getChineseZodiac(int year) {
        if (year < 1900) {
            return "未知";
        }
        int start = 1900;
        String[] years = new String[]{
                "鼠", "牛", "虎", "兔",
                "龙", "蛇", "马", "羊",
                "猴", "鸡", "狗", "猪"
        };
        return years[(year - start) % years.length];
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的星座属相测算代码,你可以根据自己的需要进行更改和完善。 ```python # 根据生日计算星座属相 def calculate_constellation_and_zodiac(birthday): # 12个星座分别对应的日期范围 constellation_date_range = [ (1, 21, 2, 19), # 水瓶座 (2, 20, 3, 20), # 双鱼座 (3, 21, 4, 20), # 白羊座 (4, 21, 5, 21), # 金牛座 (5, 22, 6, 21), # 双子座 (6, 22, 7, 22), # 巨蟹座 (7, 23, 8, 23), # 狮子座 (8, 24, 9, 23), # 处女座 (9, 24, 10, 23), # 天秤座 (10, 24, 11, 22), # 天蝎座 (11, 23, 12, 21), # 射手座 (12, 22, 1, 20) # 摩羯座 ] # 12个属相分别对应的起始年份 zodiac_start_year = [ 1900, 1912, 1924, 1936, 1948, 1960, 1972, 1984, 1996, 2008, 2020 ] year, month, day = map(int, birthday.split('-')) # 计算星座 for i in range(len(constellation_date_range)): start_month, start_day, end_month, end_day = constellation_date_range[i] if (month == start_month and day >= start_day) or (month == end_month and day <= end_day) or (start_month < month < end_month): constellation = i break else: constellation = 11 # 默认为摩羯座 # 计算属相 zodiac = (year - zodiac_start_year[0]) % 12 return constellation, zodiac ``` 使用方法: ```python birthday = '1994-02-08' constellation, zodiac = calculate_constellation_and_zodiac(birthday) print(f'星座为{constellation + 1},属相为{zodiac + 1}') ``` 输出结果: ``` 星座为5,属相为10 ``` 说明该生日对应的星座为双子座,属相为蛇。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值