梅花时间起卦小程序(基于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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是一段C++代码,可以根据梅花易数的年月日时起卦法,生成对应的六十四卦: ```c++ #include <iostream> #include <string> #include <vector> using namespace std; const vector<string> heavenlyStems = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}; const vector<string> earthlyBranches = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}; const vector<string> trigrams = {"乾", "坤", "震", "巽", "坎", "离", "艮", "兑"}; int main() { int year, month, day, hour; cout << "请输入年月日时(格式为:年 月 日 时):" << endl; cin >> year >> month >> day >> hour; int y = (year - 1900) % 60 + 1; int m = month; int d = day; int h = static_cast<int>(hour / 2.0 + 0.5) % 12; string upperTri = heavenlyStems[y % 10] + earthlyBranches[y % 12]; string middleTri = heavenlyStems[m % 10] + earthlyBranches[m % 12]; string lowerTri = heavenlyStems[d % 10] + earthlyBranches[d % 12]; int upperIndex = (y - 1) % 8; int middleIndex = (m - 1) % 8; int lowerIndex = (d - 1) % 8; string gua = trigrams[upperIndex] + trigrams[middleIndex] + trigrams[lowerIndex]; cout << "卦象为:" << gua << endl; return 0; } ``` 这段代码的思路是首先根据输入的年月日时,计算出对应的天干地支,然后根据天干地支计算出对应的上、中、下卦,并根据上中下卦的三个爻的变化情况,确定对应的六十四卦。具体实现中使用了梅花易数的年月日时起卦法。最后输出生成的卦象。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值