【20240326 计算星座】

学习时间:

2024.3.26

学习目标:

  • 输入日期,计算星座;


学习内容B:

java练习

  • 根据输入的日期,判断星座。
    ex:输入一个日期,如20230326,程序处理后,输出2023年3月26日是XX星座。
    该题目主要练习①前后台交互;②条件分支;③异常输入数据判断。

学习产出B:

  • CheckDate .java
  • Constellation.java
  • ConstMain.java
  • 向AI学习一段代码(注释留存)

  • CheckDate .java
package com.Constellation;

public class CheckDate {
    /**
     * 检查一个字符串是否表示一个有效的日期。
     * - 字符串必须是8位数;
     * - 年份在1901到2199之间;
     * - 月份在1到12之间;

     * @param str 待检查的日期字符串。
     * @return 如果字符串表示一个有效日期,则返回true;否则返回false。
     */
    public boolean isDate(String str) {
        // 首先检查字符串是否为null和长度是否为8,是否全是数字
        if (!(str == null) && (str.length() == 8)) {
            try {
                Integer.parseInt(str);  // 尝试将字符串解析为整数,以验证其是否全为数字
            } catch (NumberFormatException e) {
                return false;  // 如果解析失败,则字符串不是有效的日期
            }
        } else {
            return false;  // 字符串为null或长度不为8,不是有效的日期
        }

        // 解析年、月、日
        int year = Integer.parseInt(str.substring(0, 4));
        int month = Integer.parseInt(str.substring(4, 6));
        int day = Integer.parseInt(str.substring(6, 8));

        // 检查年份范围
        if (!(1901 < year && year < 2199)) {
            return false;
        }
        // 检查月份范围
        if (!(1 <= month && month <= 12)) {
            return false;
        }

        // 根据月份检查日期范围
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                // 以上月份为31天
                if (!(1 <= day && day <= 31)) {
                    return false;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                // 以上月份为30天
                if (!(1 <= day && day <= 30)) {
                    return false;
                }
                break;
            case 2:
                // 二月份的日期处理,考虑闰年
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                    // 闰年,二月有29天
                    if (!(1 <= day && day <= 29)) {
                        return false;
                    }
                } else {
                    // 平年,二月有28天
                    if (!(1 <= day && day <= 28)) {
                        return false;
                    }
                }
                break;
        }
        return true;  // 字符串表示的日期满足所有条件,被认为是有效的
    }
    /*----------------------------------------------------------------
    下面是AI优化版本:
    public class DateValidator {
    private static final int MIN_YEAR = 1901;
    private static final int MAX_YEAR = 2199;
    private static final int[] DAYS_IN_MONTH = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public boolean isDate(String str) {
        // 初步检查字符串是否为null或长度是否为8
        if (str == null || str.length() != 8) {
            return false;
        }
        // 尝试解析字符串为整数
        try {
            int year = Integer.parseInt(str.substring(0, 4));
            int month = Integer.parseInt(str.substring(4, 6));
            int day = Integer.parseInt(str.substring(6, 8));
            // 检查年份、月份和日期是否有效
            return isValidDate(year, month, day);
        } catch (NumberFormatException e) {
            // 如果解析失败,返回false
            return false;
        }
    }
    private boolean isValidDate(int year, int month, int day) {
        // 检查年份范围
        if (year < MIN_YEAR || year > MAX_YEAR) {
            return false;
        }
        // 检查月份范围
        if (month < 1 || month > 12) {
            return false;
        }
        // 检查日期范围
        if (day < 1 || day > DAYS_IN_MONTH[month - 1]) {
            return false;
        }
        // 对二月进行额外的检查以处理闰年
        if (month == 2) {
            return isLeapYear(year) ? day <= 29 : day <= 28;
        }
        return true;
    }
    private boolean isLeapYear(int year) {
        // 检查是否为闰年
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
}
    AI版结束 */
}


  • Constellation.java
package com.Constellation;

public class Constellation {
//	水瓶座:1月20日~2月18日
//	双鱼座:2月19日~3月20日
//  白羊座:3月21日~4月19日
//  金牛座:4月20日~5月20日
//  双子座:5月21日~6月21日
//  巨蟹座:6月22日~7月22日
//  狮子座:7月23日~8月22日
//  处女座:8月23日~9月22日
//  天秤座:9月23日~10月23日
//  天蝎座:10月24日~11月22日
//  射手座:11月23日~12月21日
//	摩羯座:12月22日~1月19日
	public String calculateConstellation(int month, int day) {
		if (month == 1) {
			if (day >= 20) {
				return "水瓶座";
			} else {
				return "摩羯座";
			}
		} else if (month == 2) {
			if (day >= 19) {
				return "双鱼座";
			} else {
				return "水瓶座";
			}
		} else if (month == 3) {
			if (day >= 21) {
				return "白羊座";
			} else {
				return "双鱼座";
			}
		} else if (month == 4) {
			if (day >= 20) {
				return "金牛座";
			} else {
				return "白羊座";
			}
		} else if (month == 5) {
			if (day >= 21) {
				return "双子座";
			} else {
				return "金牛座";
			}
		} else if (month == 6) {
			if (day >= 22) {
				return "巨蟹座";
			} else {
				return "双子座";
			}
		} else if (month == 7) {
			if (day >= 23) {
				return "狮子座";
			} else {
				return "巨蟹座";
			}
		} else if (month == 8) {
			if (day >= 23) {
				return "处女座";
			} else {
				return "狮子座";
			}
		} else if (month == 9) {
			if (day >= 23) {
				return "天秤座";
			} else {
				return "处女座";
			}
		} else if (month == 10) {
			if (day >= 24) {
				return "天蝎座";
			} else {
				return "天秤座";
			}
		} else if (month == 11) {
			if (day >= 23) {
				return "射手座";
			} else {
				return "天蝎座";
			}
		} else if (month == 12) {
			if (day >= 22) {
				return "摩羯座";
			} else {
				return "射手座";
			}
		} else {
			return null;
		}
	}
	/*AI版:
	 		private static final int[] ZODIAC_BOUNDARIES = {20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22};	   
	        // 注意:由于数组下标从0开始,所以需要减去1
	        int index = month - 1;
	        if (day >= ZODIAC_BOUNDARIES[index]) {
	            index++; // 进入下一个星座区间
	        }	
	        // 根据调整后的索引得到星座名称
	        switch (index % 12) {
	            case 0: return "水瓶座";
	            case 1: return "摩羯座";
	            case 2: return "双鱼座";
	            case 3: return "白羊座";
	            case 4: return "金牛座";
	            case 5: return "双子座";
	            case 6: return "巨蟹座";
	            case 7: return "狮子座";
	            case 8: return "处女座";
	            case 9: return "天秤座";
	            case 10: return "天蝎座";
	            case 11: return "射手座";
	            default: return null; // 不应该到达此处,但以防万一添加此返回值
	        }
	AI版结束*/

	/**
	 * 根据给定的日期获取对应的星座。
	 */
	public String getConstellation(String date) {
		// 从日期字符串中提取月份和日
		String month = date.substring(4, 6);
		String day = date.substring(6, 8);
		// 调用Constellation方法,根据月份和日返回对应的星座字符串
		return calculateConstellation(Integer.parseInt(month), Integer.parseInt(day));
	}

}


  • ConstMain.java
package com.Constellation;

import java.util.Scanner;

public class ConstMain {
    public static void main(String[] args) {
        CheckDate cd = new CheckDate();
        Constellation cs = new Constellation();
        Scanner sc = new Scanner(System.in);
        String date = "";
        System.out.println("输入8位日期,将计算这天所属的星座,如20240326:");
        while (!date.equals("over")) {
            date = sc.nextLine();
            if (cd.isDate(date)) {
                System.out.println("您输入的日期是:" + date);
                System.out.println("经计算,这天所属的星座是:" + cs.getConstellation(date));
                System.out.println("可以继续计算,结束请输入“over”:");
            } else if (date.equals("over")) {
                System.out.println("计算结束");
            } else {
                System.out.println("您输入的日期格式不正确,请重新输入8位日期,如20240326");
            }
        }
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值