作業:輸入某年某月某日,判斷這一天是這一年的第幾天?,要求:需寫一個函式,給定年月 日,求的該天處于該年的第幾天,然后在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;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/59600.html
標籤:C#
上一篇:自增ID演算法snowflake
下一篇:C# IoC學習筆記
