习惯在使用年份计算日期的同时,日常我们还会有通过十二生肖记年的方式,十二生肖的故事广为流传,那么,在c#中我们如何判断某年是属于哪个生肖呢?下面,济南云服务器小编就来带大家一起详细看看,怎么通过编码实现判断某年是什么生肖?有兴趣的朋友可以过来参考一下
关键代码:
public class ChineseZodiac { private string[] zodiacs = { "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" }; public string GetZodiac(int year) { int index = (year - 1900) % 12; return zodiacs[index]; } } internal class Program { static void Main(string[] args) { ChineseZodiac zodiac = new ChineseZodiac(); int year = 2026; string zodiacYear = zodiac.GetZodiac(year); Console.WriteLine($"{year}年的生肖是:{zodiacYear}"); Console.ReadKey(); } }
评论