无Bug,Java语言编写根据出生日期判断属相和星座

根据出生日期判断属相和星座,Java语言编写,无bug

习题代码

package com.newer.oop;

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * author: HCQ
 * date: 2018/7/26 
 * see: 1. 根据生日判断属相和星座 2. 年份分为闰年和平年 3. 判断月份和日期是否输入正确
 */
public class Birthday {
    static int year;
    private static Scanner sc;
    static boolean dayPanDuan; // 判断日期输入是否正确,月份天数正确为true,错误为false
    /* 根据年份判断属相 */

    public static String ShuXiang(int year) {
        String sx = null;
        switch (year % 12) {
        case 4:
            sx = "鼠";
            break;
        case 5:
            sx = "牛";
            break;
        case 6:
            sx = "虎";
            break;
        case 7:
            sx = "兔";
            break;
        case 8:
            sx = "龙";
            break;
        case 9:
            sx = "蛇";
            break;
        case 10:
            sx = "马";
            break;
        case 11:
            sx = "羊";
            break;
        case 0:
            sx = "猴";
            break;
        case 1:
            sx = "鸡";
            break;
        case 2:
            sx = "狗";
            break;
        case 3:
            sx = "猪";
            break;
        }
        return sx;
    }

    /* 根据月份和日期判断星座 */
    public static String XingZuo(int year, int month, int day) {
        String xz = null;
        if ((month == 3 && day >= 21 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {
            xz = "白羊";
        }
        if ((month == 4 && day >= 20 && day <= 30) || (month == 5 && day >= 1 && day <= 20)) {
            xz = "金牛";
        }
        if ((month == 5 && day >= 21 && day <= 31) || (month == 6 && day >= 1 && day <= 21)) {
            xz = "双子";
        }
        if ((month == 6 && day >= 22 && day <= 30) || (month == 7 && day >= 1 && day <= 22)) {
            xz = "巨蟹";
        }
        if ((month == 7 && day >= 23 && day <= 31) || (month == 8 && day >= 1 && day <= 22)) {
            xz = "狮子";
        }
        if ((month == 8 && day >= 23 && day <= 31) || (month == 9 && day >= 1 && day <= 22)) {
            xz = "处女";
        }
        if ((month == 9 && day >= 23 && day <= 30) || (month == 10 && day >= 1 && day <= 23)) {
            xz = "天秤";
        }
        if ((month == 10 && day >= 24 && day <= 31) || (month == 11 && day >= 1 && day <= 22)) {
            xz = "天蝎";
        }
        if ((month == 11 && day >= 23 && day <= 30) || (month == 12 && day >= 1 && day <= 21)) {
            xz = "射手";
        }
        if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) {
            xz = "摩羯";
        }
        if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 18)) {
            xz = "水瓶";
        }
        if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { // 闰年2月29天
            if ((month == 2 && day >= 19 && day <= 29) || (month == 3 && day >= 1 && day <= 20)) {
                xz = "双鱼";
            }
        } else { // 平年2月28天
            if ((month == 2 && day >= 19 && day <= 28) || (month == 3 && day >= 1 && day <= 20)) {
                xz = "双鱼";
            }
        }
        return xz;
    }

    /* 判断日期是否输入正确 */
    public static void PanDuanday(int year, int month, int day) {
        switch (month) {
        case 1:
            if (day >= 1 && day <= 31) { // 日期在该月份中,dayPanDuan为ture
                dayPanDuan = true;
                break;
            } else { // 日期在该月份中,dayPanDuan为false
                dayPanDuan = false;
                break;
            }
        case 2:
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                if (day >= 1 && day <= 29) {
                    dayPanDuan = true;
                    break;
                } else {
                    dayPanDuan = false;
                    break;
                }
            } else {
                if (day >= 1 && day <= 28) {
                    dayPanDuan = true;
                    break;
                } else {
                    dayPanDuan = false;
                    break;
                }
            }
        case 3:
            if (day >= 1 && day <= 31) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 4:
            if (day >= 1 && day <= 30) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 5:
            if (day >= 1 && day <= 31) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 6:
            if (day >= 1 && day <= 30) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 7:
            if (day >= 1 && day <= 31) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 8:
            if (day >= 1 && day <= 31) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 9:
            if (day >= 1 && day <= 30) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 10:
            if (day >= 1 && day <= 31) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 11:
            if (day >= 1 && day <= 30) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        case 12:
            if (day >= 1 && day <= 31) {
                dayPanDuan = true;
                break;
            } else {
                dayPanDuan = false;
                break;
            }
        }
    }

    /* main方法 */
    public static void main(String[] args) {
        sc = new Scanner(System.in);
        System.out.println("请输入");
        System.out.print("年: ");
        try {
            year = sc.nextInt(); // 输入年份
            System.out.print("月: ");
            int month = sc.nextInt(); // 输入月份
            if (month >= 1 && month <= 12) { // 判断月份是否输入正确,是否在1-12中
                System.out.print("日: ");
                int day = sc.nextInt(); // 输入日期
                PanDuanday(year, month, day); // 调用判断日期的方法
                if (dayPanDuan == true) { // 日期在该月份中,为ture
                    System.out.println(year + "/" + month + "/" + day + " " + "属相: " + ShuXiang(year) + ", " + "星座: "
                            + XingZuo(year, month, day)); // 输出打印年月日,属相和星座
                } else { // 日期不在该月份中,为false
                    System.out.println("输入日期有误。");
                }
            } else { // 月份不在1-12中
                System.out.println("输入月份有误。");
            }
        } catch (InputMismatchException e) {
            System.out.println("输入非法。");
        } catch(Exception e) {
            System.out.println("其他错误。");
            e.printStackTrace();
        } finally {
            System.out.println("程序结束!");
            System.exit(0);   //退出系统
        }
    }
}

我的总结

  1. 该题主要运用了switch语句,if else语句, try catch finally语句
  2. 此题解决了年份,月份和日期输入非法的BUG
    a. 年份,月份,日期输入非数字会报“输入非法。”的提示
    b. 月份只能在1-12中,否则报“输入月份有误。”的提示
    c. 日期只能是该月份的天数
    1、3、5、7、8、10、12月31天
    4、6、9、11 月30天
    2月平年28天,闰年29天
    如果不在这些范围内,会提示“输入日期有误。”

答案仅供参考,小萌新初次写博客,大神勿喷。

2018/8/3

你可能感兴趣的:(JavaOOP)