Java 根据日期获取星座

 

1.实现代码

import java.util.Calendar;
import java.util.Date;

public class BirthUtils {

    public static final String[] CONSTELLATION_ARR = { "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "魔羯座" };
    public static final int[] CONSTELLATION_EDGE_DAY = { 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22 };

    /**
     * 根据日期获取星座
     * @return
     */
    public static String getConstellation(Date date) {
        if (date == null) {
            return "";
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);
        if (day < CONSTELLATION_EDGE_DAY[month]) {
            month = month - 1;
        }
        if (month >= 0) {
            return CONSTELLATION_ARR[month];
        }
        return CONSTELLATION_ARR[11];
    }

    public static void main(String[] args) {
        // 2022-01-09 魔羯座
        System.out.println(getConstellation(new Date()));
    }
}

 

 

2.输出结果

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要计算十二星座,可以利用Java编程语言来实现。 首先,我们可以创建一个名为"ZodiacCalculator"的类来计算星座。在这个类中,我们可以定义一个方法,接收用户输入的出生日期(年份、月份和日期),并根据星座日期范围来确定用户属于哪个星座。 以下是一个大致的实现: ```java import java.util.Scanner; public class ZodiacCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入您的出生年份(例如:1990):"); int year = scanner.nextInt(); System.out.println("请输入您的出生月份(例如:3):"); int month = scanner.nextInt(); System.out.println("请输入您的出生日期(例如:21):"); int day = scanner.nextInt(); String zodiac = calculateZodiac(year, month, day); System.out.println("您的星座是:" + zodiac); } public static String calculateZodiac(int year, int month, int day) { if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) { return "水瓶座"; } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) { return "双鱼座"; } else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) { return "白羊座"; } else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) { return "金牛座"; } else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) { return "双子座"; } else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) { return "巨蟹座"; } else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) { return "狮子座"; } else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) { return "处女座"; } else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) { return "天秤座"; } else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) { return "天蝎座"; } else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) { return "射手座"; } else { return "摩羯座"; } } } ``` 在这个实现中,我们定义了一个静态方法`calculateZodiac`来计算星座并返回结果。根据用户输入的日期进行判断,如果符合日期范围,则返回相应的星座名称。最后,在`main`方法中获取用户输入的出生日期,调用`calculateZodiac`方法计算星座,并将结果输出到控制台中。 这样,我们就可以使用这个Java程序来计算出用户的星座了。通过用户输入的出生日期,程序会返回对应的星座名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yixian123.com

谢谢打赏,祝老板心想事成

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值