梅花时间起卦小程序(基于Java基础)

在梅花易数中有一种起卦方式是用日期起卦

年月日时起例
年月日为上卦。年月日加时总数为下卦。又以年月日时总数取爻。如子年一数,丑年二数,直至亥年十二数。月如正月一数,直至十二月,亦作十二数。日数如初一一数,直至三十日,为三十数。以上年月日共计几数,以八除之,以余数作卦。如子时一数直至亥时十二数,就将年月日数加时之数,总计几数,以八除之,余数作下卦;以六除,余数作动爻。

以下为主计算代码

/**
     * 带入年月日时进行计算
     * @param year
     * @param month
     * @param day
     * @param hour
     */
    public static void Divination(int year, int month, int day, int hour){
        int up,down,Dynamic;
        if ((year+month+day)%8==0){up = 8;}else{up =(year+month+day)%8;}//计算上卦
        if ((year+month+day+hour)%8==0){down = 8;}else {down = (year+month+day+hour)%8;}//计算下卦
        if ((year+month+day+hour)%6==0){Dynamic =6;}else {Dynamic=(year+month+day+hour)%6;}//计算爻动

        System.out.print("上"+_8g[up]);
        System.out.print("下"+_8g[down]);
        System.out.println("爻动"+Dynamic);//
    }

 对年数进行计算,取个位加1为年数

/**
     * 将获取的年转换为对应的年数
     * @param year
     * @return
     */
    public static int Year(int year){
        year=year%10+1;
        return year;
    }

将时间转换为对应时辰

两个小时为一个时辰23:00-1:00为子时对应时辰数为1,1:00-2:00为丑时对应时辰数为2

以此类推

/**
     * 将获取的时间转换为时辰
     * @param hour
     * @return
     */
    public static int Chronology(double hour){
        if (hour>=1&&hour<3){
            hour=2;
        }else if (hour>=3&&hour<5){
            hour=3;
        }else if (hour>=5&&hour<7){
            hour=4;
        }else if (hour>=7&&hour<9){
            hour=5;
        }else if (hour>=9&&hour<11){
            hour=6;
        }else if (hour>=11&&hour<13){
            hour=7;
        }else if (hour>=13&&hour<15){
            hour=8;
        }else if (hour>=15&&hour<17){
            hour=9;
        }else if (hour>=17&&hour<19){
            hour=10;
        }else if (hour>=19&&hour<21){
            hour=11;
        }else if (hour>=21&&hour<23) {
            hour=12;
        }else {
            hour=1;
        }
        int h =(int)hour;
        return h;
    }

主方法

由用户选择手动输入或带入当前时间进行计算

public static void main(String[] args) {
        /**
         * 由用户选则动输入指定时间日期或用当前时间日期带入
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("输入1手动输入年月日时");
        System.out.println("输入2自动获取当前时间");
        int input = sc.nextInt();
        if (input==1){
            System.out.println("请输入年");
            year = Year(sc.nextInt());
            System.out.println("请输入月");
            month = sc.nextInt();
            System.out.println("请输入日");
            day = sc.nextInt();
            System.out.println("请输入时");
            hour =Chronology(sc.nextDouble());
            year = Year(year);//获取年数
            hour =Chronology(hour);//获取时辰数
            Divination(year,month,day,hour);//传入计算
        }//手动输入
        if (input==2){
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DATE);
            hour = calendar.get(Calendar.HOUR_OF_DAY);
            year = Year(year);//获取年数
            hour =Chronology(hour);//获取时辰数
            Divination(year,month,day,hour);//传入计算
        }//自动输入
//        System.out.println(year);
//        System.out.println(hour);
    }

以下为整体代码

package onw;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Scanner;

/**
 * 起卦
 */
public class PlumBlossom {
    public static int year, month,day,hour;
    public static void main(String[] args) {
        /**
         * 由用户选则动输入指定时间日期或用当前时间日期带入
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("输入1手动输入年月日时");
        System.out.println("输入2自动获取当前时间");
        int input = sc.nextInt();
        if (input==1){
            System.out.println("请输入年");
            year = Year(sc.nextInt());
            System.out.println("请输入月");
            month = sc.nextInt();
            System.out.println("请输入日");
            day = sc.nextInt();
            System.out.println("请输入时");
            hour =Chronology(sc.nextDouble());
            year = Year(year);//获取年数
            hour =Chronology(hour);//获取时辰数
            Divination(year,month,day,hour);//传入计算
        }//手动输入
        if (input==2){
            Calendar calendar = Calendar.getInstance();
            year = calendar.get(Calendar.YEAR);
            month = calendar.get(Calendar.MONTH);
            day = calendar.get(Calendar.DATE);
            hour = calendar.get(Calendar.HOUR_OF_DAY);
            year = Year(year);//获取年数
            hour =Chronology(hour);//获取时辰数
            Divination(year,month,day,hour);//传入计算
        }//自动输入
//        System.out.println(year);
//        System.out.println(hour);
    }

    /**
     * 带入年月日时进行计算
     * @param year
     * @param month
     * @param day
     * @param hour
     */
    public static void Divination(int year, int month, int day, int hour){
        int up,down,Dynamic;
        if ((year+month+day)%8==0){up = 8;}else{up =(year+month+day)%8;}//计算上卦
        if ((year+month+day+hour)%8==0){down = 8;}else {down = (year+month+day+hour)%8;}//计算下卦
        if ((year+month+day+hour)%6==0){Dynamic =6;}else {Dynamic=(year+month+day+hour)%6;}//计算爻动

        System.out.print("上"+_8g[up]);
        System.out.print("下"+_8g[down]);
        System.out.println("爻动"+Dynamic);//
    }

    /**
     * 将获取的年转换为对应的年数
     * @param year
     * @return
     */
    public static int Year(int year){
        year=year%10+1;
        return year;
    }

    /**
     * 将获取的时间转换为时辰
     * @param hour
     * @return
     */
    public static int Chronology(double hour){
        if (hour>=1&&hour<3){
            hour=2;
        }else if (hour>=3&&hour<5){
            hour=3;
        }else if (hour>=5&&hour<7){
            hour=4;
        }else if (hour>=7&&hour<9){
            hour=5;
        }else if (hour>=9&&hour<11){
            hour=6;
        }else if (hour>=11&&hour<13){
            hour=7;
        }else if (hour>=13&&hour<15){
            hour=8;
        }else if (hour>=15&&hour<17){
            hour=9;
        }else if (hour>=17&&hour<19){
            hour=10;
        }else if (hour>=19&&hour<21){
            hour=11;
        }else if (hour>=21&&hour<23) {
            hour=12;
        }else {
            hour=1;
        }
        int h =(int)hour;
        return h;
    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值