问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:days.cpp
*作 者:单昕昕
*完成日期:2015年2月6日
*版 本 号:v1.0
*
*问题描述:定义一个函数,其参数为年、月、日的值,返回这一天为该年的
第几天。要求在main函数中输入年
月日,然后调用这个函数求值,并在main函数中输出结果。
*程序输出:这一天为该年的第几天。
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test
{
class Program
{
static int days(int y, int m, int d)
{
int sum = 0, n;
for (n = 1; n < m; ++n)
{
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
{
if (n == 1 || n == 3 || n == 5 || n == 7 || n == 8 || n == 10 || n == 12)
sum += 31;
else if (n == 2)
sum += 29;
else
sum += 30;
}
else
{
if (n == 1 || n == 3 || n == 5 || n == 7 || n == 8 || n == 10 || n == 12)
sum += 31;
else if (n == 2)
sum += 28;
else
sum += 30;
}
}
sum += d;
return (sum);
}
static void Main()
{
int year, month, day;
Console.Write("输入年份:");
year = int.Parse(Console.ReadLine());
Console.Write("输入月份:");
month = int.Parse(Console.ReadLine());
Console.Write("输入日期:");
day = int.Parse(Console.ReadLine());
Console.WriteLine("这是该年的第{0}天。", days(year, month, day));
Console.ReadLine();
}
}
}
运行结果:
知识点总结:
练习使用days方法。
学习心得:
一般都是用静态static。知识点总结:
乘法口诀表。
学习心得: