编写一个C#控制台应用程序,从键盘输入年份,判断此年份是不是闰年。

2024-05-14

1. 编写一个C#控制台应用程序,从键盘输入年份,判断此年份是不是闰年。

1、打开编译软件visual studio2010,找到起始界面的菜单栏,选择“文件”->“新建”->“项目”。


2、在新建项目页面中,选择创建一个C#的控制台程序,具体创建如下图的红框框中部分。给项目命名为“IsRunNian”。



3、用Console.Write方法输入提示信息,Console.Write("请输入你要判断的年份:");
将输入的信息存储到strnian这个字符串中,stringstrnian=Console.ReadLine();


4、将输入的信息转化成整数,并判断输入的数是不是在大于等于0的正整数,如果不是则提示“输入的信息不符合要求”。
可以看到,输入-2121弹出输入信息不符合要求。


5、判断输入的年是否是闰年,能被4整除但是不能被100整除的是闰年;

能被400整除的是闰年。具体实现代码如下图,红框框中的就是闰年实现的代码。


6、编译运行程序,可以来输入自己不同年份来检测程序功能。当输入2000年的时候提示是闰年。


7、程序功能检测,输入2200,提示不是闰年。这和实际情况符合,功能完美实现。

编写一个C#控制台应用程序,从键盘输入年份,判断此年份是不是闰年。

2. 用C#写程序”输入年月确定后显示这个月有几天(注意润年和平年)”

具体的验证输入信息对不对我可就不加了 比如输入信息合法性什么的
  希望这个对你有帮助 主要DateTime没有DayOfWeek 和DayOfYear
  主要的还是告诉你 不论你写什么 一定要模块化
  
 using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
 namespace DayOfMonth
  {
  class Program
  {
// http://zhidao.baidu.com/question/81922821.html?fr=id_push
  // 用C#写程序”输入年月确定后显示这个月有几天(注意润年和平年)”
  // 100整除并且不能被400整除的年数不是闰年,除此之外只要能被4和400整除的就是闰年
  static void Main(string[] args)
  {
  string example = "This app will return Days of Month want you want, please give some info (includes Year and Month) such as below:\
Please input Year :\
2008\
Please input Month :\
1\
Days of 2008.1 is 31";
  string noticeYear = "Please input Year :";
  string noticeMonth = "Please input Month :";
  string expectedYear = string.Empty;
  string expectedMonth = string.Empty;
  uint daysOfMonth = uint.MinValue;
  
 Console.WriteLine("{0}", example);
  
 Console.WriteLine("{0}", noticeYear);
  expectedYear = Console.ReadLine();
  
 Console.WriteLine("{0}", noticeMonth);
  expectedMonth = Console.ReadLine();
  
 daysOfMonth = GetDaysOfMonth(GetTypeOfMoMonth(expectedMonth), GetTypeOfYear(expectedYear));
  
 Console.WriteLine("Days of {0} in {1} is {2}", expectedMonth, expectedYear, daysOfMonth.ToString());
  Console.ReadLine();
  }
  
 public enum DayInMonth : uint
  {
  January = 31,
  February = 28,
  FebruaryLeapYear = 29,
  March = 31,
  April = 30,
  May = 31,
  June30,
  July = 31,
  August = 31,
  September = 30,
  October = 31,
  November = 30,
  December = 31
  }
  
 public enum TypeOfYear : uint
  {
  LeapYear,
  NonLeapYear
  }
  
 public static uint GetDaysOfMonth(DayInMonth month, TypeOfYear year)
  {
  uint daysOfMonth = uint.MinValue;
  
 daysOfMonth = Convert.ToUInt32(month);
  
 if (year.Equals(TypeOfYear.LeapYear) && month.Equals(DayInMonth.February))
  {
  daysOfMonth = Convert.ToUInt32(DayInMonth.FebruaryLeapYear);
  }
  
 return daysOfMonth;
  }
  
 public static TypeOfYear GetTypeOfYear(string expectedYear)
  {
  TypeOfYear typeOfYear = TypeOfYear.NonLeapYear;
  int formatYear = int.MinValue;
  
 formatYear = Convert.ToInt32(expectedYear);
  
 if ((0 == (formatYear % 4)) && (0 == (formatYear % 400)))
  {
  typeOfYear = TypeOfYear.LeapYear;
  }
  
 return typeOfYear;
  }
  
 public static DayInMonth GetTypeOfMoMonth(string expectedMonth)
  {
  DayInMonth typeOfMonth = DayInMonth.January;
  int formatMonth = int.MinValue;
  
 formatMonth = Convert.ToInt32(expectedMonth);
  
 switch (formatMonth)
  {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12:
  typeOfMonth = DayInMonth.January;
  break;
  case 4:
  case 6:
  case 9:
  case 11:
  typeOfMonth = DayInMonth.April;
  break;
  case 2:
  typeOfMonth = DayInMonth.February;
  break;
  default:
  Console.WriteLine("Error type of Month, no matched item");
  break;
  }
  
 return typeOfMonth;
  }
  }
  }

3. C#中 如何表示昨天

==========
//获取昨天的日期,只要用今天的日期-1就可以了
DateTime.Now是获取今天时间
下面重载了时间的-方法只要调用Date.operater -就可以获得昨天日期了
#include
using namespace std;
int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
class Date
{
 int year,month,day;
 bool leap(int);
 int dton(Date &);
 Date ntod(int);
public:
 Date() {}          //默认构造函数;
 Date(int y,int m,int d) {year=y;month=m;day=d;}//重载构造函数;
 Date(const Date &d)        //复制构造函数;
 {
  year=d.year;
  month=d.month;
  day=d.day;
 }
 Date operator+(int days)      //返回一日期加上天数得到的日期;
 {
  static Date date;
  int number=dton(*this)+days;
  date=ntod(number);
  return date;
 }
 Date operator-(int days)     //返回一日期减去天数得到的日期;
 {
  static Date date;
  int number=dton(*this)-days;
  date=ntod(number);
  return date;
 }
 int operator-(Date &b)      //返回两日期相差的天数;
 {
  int days=dton(*this)-dton(b)-1;
  return days;
 }
 bool operator < (const Date d)
 {
  if((year<d.year) || (year==d.year && month<d.month) || (year==d.year && month==d.month && day<d.day))
   return true;
  else
   return false;
 }
 bool operator == (const Date d)
 {
  if((year==d.year && month==d.month && day==d.day))
   return true;
  else
   return false;
 }
 bool operator > (const Date d)
 {
  if((year>d.year) || (year==d.year && month>d.month) ||(year==d.year && month==d.month && day>d.day))
   return true;
  else 
   return false;
 }
 void getDate()
 {
  cout<<"请输入日期:"<<endl;
  cout<<"年份:";
  cin>>year;
  cout<<"月份:";
  cin>>month;
  cout<<"日期:";
  cin>>day;
 }
 void display() {cout<<year<<"."<<month<<"."<<day<<endl;}
};
bool Date::leap(int year)       //判断指定的年份日否为闰年;
{
 if(year%4==0 && year%100!=0 || year%400==0)
  return true;
 else
  return false;
}
int Date::dton(Date &d)       //将指定的日期转换成从0年0月0日起的天数;
{
 int y,m,days=0;
 for(y=1;y<=d.year;y++)
  if(leap(y)) days+=366;
  else days+=365;
 for(m=0;m<d.month-1;m++)
  if(leap(d.year)) days+=day_tab[1][m];
  else days+=day_tab[0][m];
 days+=d.day;
 return days;
}
Date Date::ntod(int n)       //将指定的0年0月0日起的天数转换成对应的日期;
{
 int y=1,m=1,d,rest=n,lp;
 while(1)
 {
  if(leap(y))
  {
   if(rest<=366) break;
   else rest-=366;
  }
  else
  {
   if(rest<=365) break;
   else rest-=365;
  }
  y++;
 }
 y--;
 lp=leap(y);
 while(true)
 {
  if(lp)
  {
   if(rest>day_tab[1][m-1]) rest-=day_tab[1][m-1];
   else break;
  }
  else
  {
   if(rest>day_tab[0][m-1]) rest-=day_tab[0][m-1];
   else break;
  }
  m++;
 }
 d=rest;
 return Date(y,m,d);
}
void main()
{
 Date now;
 now.getDate();
 cout<<"now:";now.display();
 Date d1=now+1,d2=now-1;
 cout<<"now+1:";d1.display();
 cout<<"now-1:";d2.display();
} 
以上是自己实现的方法

系统方法是:
DateTime.Now.Date.AddDays(-1)

C#中 如何表示昨天

4. C#winform 如何将一个长的Point[]数组分割成多个Point 数组

final int LEAPYEAR = 366;
int[] YEAR = new int[LEAPYEAR];
和
int[] YEAR = new int[366];

5. 用C#控制台应用程序对NextDate问题运用决策表法设计测试用例

1. 输入是否合法 
day_list[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
// 判断条件:
( year>=1920 && year<=2050 ) && 
(
 ( month>=1 && month=1 && day<=day_list[month-1] )||
 ( month==2 && isLeap(year) && day>=1 && day<=29 ) 
)
2. 
/*
函数说明: int NextDate(int month,int day,int year)
如果还有明天,返回1;
如果输入不合法,返回-1;
如果超出处理范围,返回-2
*/
int bLeapYear;
int newY,newM,newD;
newY = year;
newM = month;
newD = day;
bLeapYear = ( (year%4)==0 && (year%100)!=0 ) || (year%400)==0;
if ( year>=1920 && year<=2050 ) 
{
  if ( month>=1 && month=1 )
  {
     if ( day<day_list[month-1] )
     {
       newD += 1;
     }  
     else if ( day==day_list[month-1] && !bLeapYear )
     {
       if ( month==12 && day==31 )
       {
         if ( year==2050 ) return -2;
         else 
         {
           newY += 1; newM = 1; newD = 1;
         }
       }
       else
       {
          newM += 1; newD = 1;
       }
     }
     else if ( month==2 && bLeapYear )
     {
        if ( day==28 ) newD = 29;
        else if( day==29 )
        { newM = 3; newD = 1; }
        else return -1;
     } else return -1;
  }
}
// 输出
return 1;
3. 测试方法:
3.1 闰年二月的最后一天
3.2 闰年二月的第28天
3.3 非闰年二月的第28天

3.4 每年的非二月的月末
3.5 每年的十二月的月末
3.6 2050年十二月的月末
3.7 
year>2050 || year12 || day<1 ||
(day>day_list[month-1] && month>=1 && month<=12 && month!=2 ) || 
(day>28 && month==2 && !bLeapYear ) ||
(day>29 && month==2 && bLeapYear ) 

3.8 闰年非二月的月末 (3.4的分支)
测试出程序错误:
4.1
else if ( day==day_list[month-1] && !bLeapYear )
应改为:
else if ( day==day_list[month-1] && month!=2 )
4.2
     else if ( month==2 && bLeapYear )
     {
        if ( day==28 ) newD = 29;
        else if( day==29 )
        { newM = 3; newD = 1; }
        else return -1;
     }
应改为:
     else if ( month==2 )
     {
        if ( day==28 && bLeapYear ) newD = 29;
        else if( day==28 && !bLeapYear || ( day==29 && bLeapYear ) )
        { newM = 3; newD = 1; }
        else return -1;
     }

用C#控制台应用程序对NextDate问题运用决策表法设计测试用例

6. c#中true的问题

bool result;

if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
           {
                result = true;
            }
            else

               result = false;

if (result)
            {
                Console.WriteLine("闰年");
            }
            else
            {
                Console.WriteLine("不是闰年");
            }

这样能看懂吧

7. C# 中 怎样获得当前星期以及农历时间

  这是网上的例子,你仔细看看
  using System;
  using System.Data;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Globalization;


  /// 
  /// chinaDate 的摘要说明
  /// 
  public class chinaDate
  {
  public string cDate()
  {
  ChineseLunisolarCalendar l = new ChineseLunisolarCalendar();
  DateTime dt = DateTime.Today; //转换当日的日期
  //dt = new DateTime(2006, 1,29);//农历2006年大年初一(测试用),也可指定日期转换
  string[] aMonth ={"","正月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "腊月", "腊月" };
  //a10表示日期的十位!
  string[] a10 ={ "初", "十", "廿", "卅" };
  string[] aDigi ={ "O", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
  string sYear = "", sYearArab = "", sMonth = "", sDay = "", sDay10 = "", sDay1 = "", sLuniSolarDate = "";
  int iYear, iMonth, iDay;
  iYear = l.GetYear(dt);
  iMonth = l.GetMonth(dt);
  iDay = l.GetDayOfMonth(dt);

  //Format Year
  sYearArab = iYear.ToString();
  for (int i = 0; i < sYearArab.Length; i++)
  {
  sYear += aDigi[Convert.ToInt16(sYearArab.Substring(i, 1))];
  }

  //Format Month
  int iLeapMonth = l.GetLeapMonth(iYear); //获取闰月


  if (iLeapMonth > 0 && iMonth<=iLeapMonth)
  {
  //for (int i = iLeapMonth + 1; i < 13; i++) aMonth[i] = aMonth[i - 1];
  aMonth[iLeapMonth] = "闰" + aMonth[iLeapMonth-1];
  sMonth = aMonth[l.GetMonth(dt)];
  }
  else if (iLeapMonth > 0 && iMonth > iLeapMonth)
  {
  sMonth = aMonth[l.GetMonth(dt) - 1];
  }
  else
  {
  sMonth = aMonth[l.GetMonth(dt)];
  }


  //Format Day
  sDay10 = a10[iDay / 10];
  sDay1 = aDigi[(iDay % 10)];
  sDay = sDay10 + sDay1;

  if (iDay == 10) sDay = "初十";
  if (iDay == 20) sDay = "二十";
  if (iDay == 30) sDay = "三十";

  //Format Lunar Date
  //sLuniSolarDate = dt.Year+"年"+dt.Month+"月"+dt.Day+"日 "+Weeks(dt.DayOfWeek.ToString())+" 农历" + sYear + "年" + sMonth + sDay;
  sLuniSolarDate =" 农历" + sMonth + sDay;
  return sLuniSolarDate;

  }
  private string Weeks(string Weeken)
  {
  switch (Weeken)
  {
  case "Monday":
  return "星期一";
  break;
  case "Tuesday":
  return "星期二";
  break;
  case "Wednesday":
  return "星期三";
  break;
  case "Thursday":
  return "星期四";
  break;
  case "Friday":
  return "星期五";
  break;
  case "Saturday":
  return "星期六";
  break;
  case "Sunday":
  return "星期日";
  break;
  default:
  return " ";
  }

  }

  }

  二、第二步

  chinaDate s = new chinaDate();

  Label1.Text = DateTime.Now.ToShortDateString() + "     " + CaculateWeekDay(DateTime.Now.DayOfWeek.ToString()) + " " + s.cDate();

  三、第三步

  #region 得到今天是星期几
  protected string CaculateWeekDay(string week)
  {
  string weekstr = "";
  switch (week)
  {
  case "Monday": weekstr = "星期一"; break;
  case "Tuesday": weekstr = "星期二"; break;
  case "Wednesday": weekstr = "星期三"; break;
  case "Thursday": weekstr = "星期四"; break;
  case "Friday": weekstr = "星期五"; break;
  case "Saturday": weekstr = "星期六"; break;
  case "Sunday": weekstr = "星期日"; break;
  }

  return weekstr;


  }

  #endregion

C# 中 怎样获得当前星期以及农历时间

8. C#获取服务器时间??急急急请高手帮忙..

唉,花了点时间,现做了一个,你把下面的代码保存为ASPX就可以运行了,我已经测试通过.


下面这个Hidden变量用来暂存服务器时间
"> 



  getServerTime();

//计算当前服务器时间
function getServerTime()
{
  //取得要进行显示的日期
 var datetimeYou = document.getElementById('ServerTime').value.split(" ");
 var yout1 = datetimeYou[0].split("-");
 var yout2 = datetimeYou[1].split(":");
 
 var nowY = parseFloat(yout1[0]);
 var nowM = parseFloat(yout1[1]);
 var nowD = parseFloat(yout1[2]);
 var h = parseFloat(yout2[0]);
 var m = parseFloat(yout2[1]);
 var s = parseFloat(yout2[2]);
  
  var daysMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  if(leapyear(nowY))
    daysMonth[1] += 1;//闰年,2月加1

 s = s + 1;
 if (s >= 60) 
 {
  s = 0;
  m = m + 1;
  if (m >= 60)
  {
   m = 0;
   h = h + 1;
   if (h >= 24) 
   {
     h = 0;
     nowD += 1;
     if(nowD > daysMonth[nowM-1])
     {
       nowD = 1;
       nowM += 1;
       if(nowM > 12)
       {
         nowM = 1;
         nowY += 1;
       }
     }
   }
  }
 }
 if (nowM < 10) nowM = "0" + nowM;
 if (nowD < 10) nowD = "0" + nowD;
 if (h < 10) h = "0" + h;
 if (m < 10) m = "0" + m;
 if (s < 10) s = "0" + s;
 document.getElementById('ServerTime').value = nowY + "-" + nowM + "-" + nowD + " " + h + ":" + m + ":" + s;
 
  var today,hour,second,minute,year,month,date; 
  var strDate ; 
  //注意:Javascript中的月要减1,比如现在4月,要写成3
  today=new Date(yout1[0],yout1[1]-1,yout1[2],yout2[0],yout2[1],yout2[2]);

  var n_day = today.getDay(); 
  switch (n_day) 
  { 
  case 0:{ 
  strDate = "星期日" 
  }break; 
  case 1:{ 
  strDate = "星期一" 
  }break; 
  case 2:{ 
  strDate ="星期二" 
  }break; 
  case 3:{ 
  strDate = "星期三" 
  }break; 
  case 4:{ 
  strDate = "星期四" 
  }break; 
  case 5:{ 
  strDate = "星期五" 
  }break; 
  case 6:{ 
  strDate = "星期六" 
  }break; 
  case 7:{ 
  strDate = "星期日" 
  }break; 
  } 
  year = today.getFullYear(); 
  month = today.getMonth() + 1; 
  date = today.getDate(); 
  hour = today.getHours(); 
  minute =today.getMinutes(); 
  second = today.getSeconds(); 
  if(month<10) month="0"+month; 
  if(date<10) date="0"+date; 
  if(hour<10) hour="0"+hour; 
  if(minute<10) minute="0"+minute; 
  if(second<10) second="0"+second; 
  document.getElementById('divCurrentUser').value = "当前时间:" + year + "年" + month + "月" + date + "日 " + strDate +" " + hour + ":" + minute + ":" + second; //显示时间

 setTimeout("getServerTime()",1000)
}

//判断参数是否闰年
function leapyear(year){
  if(!/[^0]\d+/.test(year)){
  //通过正则判断年份的合法性
     return false;
  }
  
  if(0==year)
   return true;
  
  if ( ((0==year%4) && !(0==year%100)) || (0==year%400) ){
     return true;
  }else{
   if(year!=0)
     return false;
  }
}