将年份转换成天干地支

import java.util.*;
public class tiangan {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner reader=new Scanner(System.in);
        String tG[]={"葵","甲","乙","丙","丁","戊","己","庚","辛","壬"};
        String dZ[]={"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};
        String sX[]={"鼠","牛","虎","兔","龙","蛇","马","羊","猴","鸡","狗","猪"};
        int year,tg,dz,sx,cherk,a=0,b=0;
        System.out.println("公元前请选1,公元后请选0");
        cherk=reader.nextInt();
        System.out.println("请输入年份:");
        year=reader.nextInt();
        if(cherk==0)
        {
            //a=(year%10-3)>=0?a:-a;     不起作用!
//            a=(year%10-3);
//            if(a<0)
//            {
//                a=-a;
//            }
            a=(year%10-3);
            if(a<0)
            {
                a+=10;
            }
            b=(year)%12;
            b+=9;
            if(b>12)
            {
                b-=12;
            }
            b-=1;
        }
        else if(cherk==1)
        {
            a=8-year%10;
            if(a<0)
            {
                a+=10;
            }
            
            b=year%12;
            b=10-b;
            if(b<=0)
            {
                b+=12;
            }
            b-=1;
        }
        else
        {
            System.out.println("输入有误");
        }
        System.out.println("天干地支:"+tG[a]+dZ[b]);
        System.out.println("生肖:"+sX[b]);
    }

}

原文地址:https://www.cnblogs.com/suHDH/p/8836269.html