Java基础公元纪年法换算天干地支纪年法

Day3.java

import java.util.Calendar;
import java.util.Scanner;

public class Days3 {
    /**
     * 公元纪年法向天干地支纪年法换算
     * 计算方法:
     * 公元年数-3,除以10余数可得天干;
     * 公元年数-3,除以12余数可得地支;
     *
     * @author ruan_luqingnian
     * @version 1.0.0
     */
    public static void main(String[] args) {
        /**
         十天干:1-甲(jiǎ)、2-乙(yǐ)、3-丙(bǐng)、4-丁(dīng)、5-戊(wù)、6-己(jǐ)、7-庚(gēng)、8-辛(xīn)、9-壬(rén)、0-癸(guǐ);
         十二地支:1-子(zǐ)、2-丑(chǒu)、3-寅(yín)、4-卯(mǎo)、5-辰(chén)、6-巳(sì)、7-午(wǔ)、8-未(wèi)、9-申(shēn)、10-酉(yǒu)、11-戌(xū)、0-亥(hài);
         */
        Calendar cal = Calendar.getInstance();
        int cYear = cal.get(Calendar.YEAR);
        int j = cYear - 3;
        int cSky = j % 10;
        int cGround = j % 12;
        String cS = sky(cSky);
        String[] arrC = ground(cGround);
        System.out.println("\033[35;4m" + "今年是" + cS + arrC[0] + arrC[1] + "年" + "\033[0m");

        Scanner sc = new Scanner(System.in);
        System.out.print("input year : ");
        while (true) {
            int year = sc.nextInt();
            int i = year - 3;
            int sky = i % 10;
            int ground = i % 12;
            String s = sky(sky);
            String[] arr = ground(ground);
            if (year == 0) {
                break;
            }
            System.out.println("\033[34;4m" + s + arr[0] + "年属" + arr[1] + "\033[0m");
        }
    }

    private static String[] ground(int ground) {
        String g = "";
        String a = "";
        String[] str = new String[2];
        switch (ground) {
            case 1:
                g = "子";
                a = "鼠";
                str = new String[]{g, a};
                break;
            case 2:
                g = "丑";
                a = "牛";
                str = new String[]{g, a};
                break;
            case 3:
                g = "寅";
                a = "虎";
                str = new String[]{g, a};
                break;
            case 4:
                g = "卯";
                a = "兔";
                str = new String[]{g, a};
                break;
            case 5:
                g = "辰";
                a = "龙";
                str = new String[]{g, a};
                break;
            case 6:
                g = "巳";
                a = "蛇";
                str = new String[]{g, a};
                break;
            case 7:
                g = "午";
                a = "马";
                str = new String[]{g, a};
                break;
            case 8:
                g = "未";
                a = "羊";
                str = new String[]{g, a};
                break;
            case 9:
                g = "申";
                a = "猴";
                str = new String[]{g, a};
                break;
            case 10:
                g = "酉";
                a = "鸡";
                str = new String[]{g, a};
                break;
            case 11:
                g = "戌";
                a = "狗";
                str = new String[]{g, a};
                break;
            case 0:
                g = "亥";
                a = "猪";
                str = new String[]{g, a};
                break;
            default:
                break;
        }
        return str;
    }

    private static String sky(int sky) {
        String s = "";
        switch (sky) {
            case 1:
                s = "甲";
                break;
            case 2:
                s = "乙";
                break;
            case 3:
                s = "丙";
                break;
            case 4:
                s = "丁";
                break;
            case 5:
                s = "戊";
                break;
            case 6:
                s = "己";
                break;
            case 7:
                s = "庚";
                break;
            case 8:
                s = "辛";
                break;
            case 9:
                s = "壬";
                break;
            case 0:
                s = "癸";
                break;
            default:
                break;
        }
        return s;
    }
}

Java基础公元纪年法换算天干地支纪年法

上一篇:数组和循环结合的小案例


下一篇:Constraint I/O Delay(一)