Java程序计算生肖

用Java程序计算出 每年的生肖属相

 


public class ChineseZodiac {

    //12生肖
    private final static String[] yearAnmName = new String[]{"羊", "猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马"};

    // 定义起始年,1991
    private final static int startYear = 1991;
    private final static int endYear = 2021;
    private static int age = 0;

    public static void main(String[] args) {
        for (int i = startYear; i < endYear; i++) {
            System.out.print(i + ":" + "年属" + subYear(i) + "\n");
            age++;
        }
        System.out.println("恭喜您,过了" + age + "年,今日还苟且于世~");
    }

    private static String subYear(int year) {
        int mk = year - startYear;
        return yearAnmName[mk % 12];
    }

}

 

您还没有登录,请先 登录或者 注册后,添加评论