一般编程中,都是讲if分支作为条件表达式来判断,但我在学习一个判断一个具体年份是否为闰年的程序中发现可以用条件运算符来做判断,很有趣,故抄录下来,以资借鉴。[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace leapyear
{
class Program
{
static void Main(string[] args)
{
Console.Write("请输入一个年份:");
string str = Console.ReadLine();
int year = Int32.Parse(str);
bool isleapyear = ((year%4==0)&&(year%100!=0)||(year%400==0));
string yesno= isleapyear?"是":"不是";
Console.WriteLine("{0}年{1}闰年",year,yesno);
}
}
}
运行结果见下图:
|