想根据输入数字C#查找日期

时间:2017-12-21 05:44:03

标签: c# datetime

我希望能够根据输入数字找到日期(1-365之间)。例如,如果输入的数字为40,则我的程序应输出 - 日期:9月:2 我尝试了下面的代码,并且能够找到除1月以外的所有月份的日期 -

static void Main(string[] args)
{
    int[] arryofdays = new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
    int num = Int32.Parse(Console.ReadLine());
    int temp = num;
    string date, month;

    for (int i = 0; i < arryofdays.Length; i++)
    {        
        temp = temp - arryofdays[i];
        if (temp < arryofdays[i + 1])
        {
            Console.WriteLine("Date:" + temp.ToString());
            Console.WriteLine("Month:" + (i+2).ToString());
            break;
        }
    }

    Console.ReadLine();
}

请帮忙!

4 个答案:

答案 0 :(得分:6)

为什么不尝试这样做;

 var datetime = new DateTime(2017, 1, 1).AddDays(40 - 1);
 var month = datetime.Month; //2
 var day = datetime.Day; //9

答案 1 :(得分:0)

尝试这样,采取行动,并检查它你能否转换为int形式,使你的程序类型安全

并检查验证日期值将介于1到365之间

     public static void Main(string[] args)
        {
            int days = 0;
            if (args.Length > 0 && int.TryParse(args[0], out days))
            {
                if (!(days < 1 || days > 366))
                {
                    DateTime date = new DateTime(DateTime.Now.Year, 1, 1).AddDays(days - 1);
                    Console.WriteLine("Date: {0}", date.Day);
                    Console.WriteLine("Date: {0}", date.Month);
                }
            }
            else
            {
                Console.WriteLine("input vlaue  is not correct or not present");
            }
  }

答案 2 :(得分:0)

你可以这样试试

static void Main(string[] args)
{
    int[] arryofdays = new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
    int num = Int32.Parse(Console.ReadLine());
    int days = 0;
    int months = 0;


    for (int i = 0; i < arryofdays.Length; i++) {
        if (num > arryofdays[i]) {
            num -= arryofdays[i]; 
            months++;            
        } else {
            days = num;
            break;
        }
    }

    Console.WriteLine("Date:" + days.ToString());
    Console.WriteLine("Month:" + (months+1).ToString());

    Console.ReadLine();
}

假设num = 40,在for循环的第一次迭代中,它将检查num > arryofdays[0]是否为40 > 31。这将返回true,因此31将从num递减,使num的值为9. months将递增。在下一次迭代(i = 1)中,它会检查num > arryofdays[1]是否9 > 28,这意味着只有9天而我们break因为我们不需要进一步。

您的输出将是

  

日期:9

     

月:2

答案 3 :(得分:0)

您的代码不适用于 JAN 月份的原因是,因为在temp = temp - arryofdays[i];声明中,如果您的输入小于31,您将获得负值。

您可以像这样修改代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

    namespace Rextester
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                //Your code goes here
                Console.WriteLine("Hello, world!");
            int[] arryofdays = new int[] {31,28,31,30,31,30,31,31,30,31,30,31};
            int num = Int32.Parse(Console.ReadLine());
            int temp = num;
            string date, month;

            for (int i = 0; i < arryofdays.Length; i++)
            {

                temp =(temp - arryofdays[i]);
                if (temp < arryofdays[i + 1] && temp >0)
                {
                    Console.WriteLine("Date:" + temp.ToString());
                    Console.WriteLine("Month:" + (i+2).ToString());
                    break;
                }else{//for handling first month
                    Console.WriteLine("Date:" +num);
                    Console.WriteLine("Month:" + 1);
                    break;
                }
            }

            Console.ReadLine();
            }
        }
    }


工作代码:http://rextester.com/PAJQ64015

希望有所帮助。