我们测试打印天干地支的甲子(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();
}
}
}