tft每日頭條

 > 科技

 > 使用c語言判斷某一年為閏年

使用c語言判斷某一年為閏年

科技 更新时间:2025-04-12 14:53:33

使用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("請輸入正确年份"); } } } }

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved