c#輸入某年某月某日,判斷這一天是這一年的第幾天?


作業:輸入某年某月某日,判斷這一天是這一年的第幾天?。要求:需寫一個函數,給定年月 日,求的該天處於該年的第幾天。然后在Main函數中測試。

 

思路:

①需要有兩個函數。一個主函數,一個Date函數用來計算天數。

②在主函數里面利用控制台輸入年月日,然后在調用Date函數.

=====由於調用函數了就傳值了,調用了就傳值了,調用了就傳值了。重要內容說三遍。所以就不用在后面給y,m,d傳值了

③由於date函數需要三個參數,所以寫date函數的時候 ,用的是  static int Date(int y,int m,int d),如果不需要參數的話就寫void

④給定一個數組用來裝12個月的天數,先把2月定為29天。int[] array = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

⑤然后再判斷二月有多少天,先用for循環 ,再判斷是閏年還是平年。如果就閏年就直接加,是平年就要把28賦給數組了在相加。

 

注:函數名首字母大寫,這是規定

       調用函數了就傳值了,調用了就傳值了,調用了就傳值了

 

代碼來源於我們的c#老師,因為我實在不曉得該怎么寫下去了。

using System;

namespace _0319_某年某月某日
{
class Program
{
static void Main(string[] args)
{
//控制台輸入年月日
Console.WriteLine("請輸入年");
int Y = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入月");
int M = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("請輸入日");
int D = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(y + "年" + m + "月" + d + "日是今年的第" + Date(y,m,d)+ "天");//在這里被調用
}

static int Date(int y,int m,int d)

{
//定義b裝天數
int b = 0;
//數組裝天數
int[] array = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//除2月外還有11個月,所以i=1,i<12
for(int i = 1; i < m; i++)
{
//閏年
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
{
b = b + array[i];
}
//平年
else
{
array[1] = 28;
b = b + array[i];
}

}
d = d + b;
//返回天數
return d;
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM