C#天干地支生肖-甲子(60年)循环打印

我们测试打印天干地支的甲子(60年)循环。

新建控制台应用程序SexagenaryYearDemo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SexagenaryYearDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.SetWindowSize(80, 62);
            string CelestialStem = "甲乙丙丁戊己庚辛壬癸";//天干
            string TerrestrialBranch = "子丑寅卯辰巳午未申酉戌亥";//地支
            string YearAnimal = "鼠牛虎兔龙蛇马羊猴鸡狗猪";//生肖
            int cIndex = 0;
            int tIndex = 0;
            int curIndex = 0;
            List<string> sexagenaryList = new List<string>();
            do
            {
                string current = "";
                //天干,索引越界时重新从0开始
                if (cIndex >= CelestialStem.Length)
                {
                    cIndex = 0;
                }
                current += CelestialStem[cIndex];
                cIndex++;

                //地支
                //生肖与地支一一对应 
                if (tIndex >= TerrestrialBranch.Length)
                {
                    tIndex = 0;
                }
                current += TerrestrialBranch[tIndex];
                string yearAnimal = YearAnimal[tIndex].ToString();
                tIndex++;
                //如果60次干支周期到达,就循环终止
                if (cIndex == 1 && tIndex == 1 && curIndex > 0)
                {
                    break;
                }
                curIndex++;

                string content = $"【{curIndex.ToString("D2")}】【生肖-{yearAnimal}】:{current}年";
                sexagenaryList.Add(content);
                Console.WriteLine(content);
            } while (true);
            System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "test.txt",
                string.Join(Environment.NewLine, sexagenaryList), Encoding.UTF8);
            Console.ReadLine();
        }
    }
}

运行如图

猜你喜欢

转载自blog.csdn.net/ylq1045/article/details/129424428