使用c语言判断某一年为闰年(编程判断某一年是否为闰年)

生活 科技 更新时间: 2024-07-07 08:19:24

使用c语言判断某一年为闰年?基于上传视频讲解,使用编程判断某一年是否为闰年的程序代码:,今天小编就来聊一聊关于使用c语言判断某一年为闰年?接下来我们就一起去研究一下吧!

使用c语言判断某一年为闰年(编程判断某一年是否为闰年)1

使用c语言判断某一年为闰年

基于上传视频讲解,使用编程判断某一年是否为闰年的程序代码:

代码1:使用if语句(If...else...)

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace leapYear{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ushort nyear; if(ushort.TryParse(textBox1.Text,out nyear)) { if ((nyear % 4 == 0 && nyear % 100 != 0) || nyear % 400 == 0) { textBox2.Text = textBox1.Text "是闰年"; } else { textBox2.Text = textBox1.Text "不是闰年"; } } else { MessageBox.Show("请输入正确年份"); } } }}

代码2:使用条件运算符(?:):条件运算符会根据布尔类型值或者布尔表达式返回冒号前后表达式中的一个,条件如果为true返回冒号前面的表达式,条件如果为false返回冒号后面的表达式:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace leapYear{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ushort nyear; if(ushort.TryParse(textBox1.Text,out nyear)) { //使用条件运算符(?:) textBox2.Text=(nyear % 4 == 0 && nyear % 100 != 0) || nyear % 400 == 0 ? textBox1.Text "是闰年" : textBox1.Text "不是闰年"; } else { MessageBox.Show("请输入正确年份"); } } }}

,