java 计算星座算法,java 依据生日计算星座

当前位置:我的异常网» 编程 » java 依据生日计算星座

java 依据生日计算星座

www.myexceptions.net  网友分享于:2013-09-06  浏览:344次

java 根据生日计算星座

public class ConstellationUtil {

public  enum Constellation {

Capricorn(1, "摩羯座"), Aquarius(2, "水瓶座"), Pisces(3, "双鱼座"), Aries(4,

"白羊座"), Taurus(5, "金牛座"), Gemini(6, "双子座"), Cancer(7, "巨蟹座"), Leo(

8, "狮子座"), Virgo(9, "处女座"), Libra(10, "天秤座"), Scorpio(11, "天蝎座"), Sagittarius(

12, "射手座");

private Constellation(int code, String chineseName) {

this.code = code;

this.chineseName = chineseName;

}

private int code;

private  String chineseName;

public int getCode() {

return this.code;

}

public String getChineseName() {

return this.chineseName;

}

}

public static final Constellation[] constellationArr = {

Constellation.Aquarius, Constellation.Pisces, Constellation.Aries,

Constellation.Taurus, Constellation.Gemini, Constellation.Cancer,

Constellation.Leo, Constellation.Virgo, Constellation.Libra,

Constellation.Scorpio, Constellation.Sagittarius,

Constellation.Capricorn

};

public static final int[] constellationEdgeDay = { 21, 20, 21, 21, 22, 22,

23, 24, 24, 24, 23, 22 };

public static String calculateConstellation(String birthday) {

if (birthday == null || birthday.trim().length() == 0)

throw new IllegalArgumentException("the birthday can not be null");

String[] birthdayElements = birthday.split("-");

if (birthdayElements.length != 3)

throw new IllegalArgumentException(

"the birthday form is not invalid");

int month = Integer.parseInt(birthdayElements[1]);

int day = Integer.parseInt(birthdayElements[2]);

if (month == 0 || day == 0 || month > 12)

return "";

month = day < constellationEdgeDay[month - 1] ? month - 1:month;

return month > 0 ? constellationArr[month - 1].getChineseName(): constellationArr[11].getChineseName();

}

}

文章评论

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值