C# 要根据输入的数字C查找日期吗#
我希望能够找到基于输入号码(1-365之间)的日期。例如,如果数字输入为40,则我的程序应输出日期:9个月:2 我已经尝试了以下代码,并且能够找到除1月以外所有月份的日期-C# 要根据输入的数字C查找日期吗#,c#,datetime,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 te
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();
}
static void Main(字符串[]args)
{
int[]arryofdays=新int[]{31,28,31,30,31,30,31,31,30,31};
int num=Int32.Parse(Console.ReadLine());
int temp=num;
字符串日期,月;
对于(int i=0;i
请帮忙 你为什么不这样试一下
var datetime = new DateTime(2017, 1, 1).AddDays(40 - 1);
var month = datetime.Month; //2
var day = datetime.Day; //9
试着这样做,取一个片段,并检查它是否可以转换为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");
}
}
publicstaticvoidmain(字符串[]args)
{
整数天=0;
if(args.Length>0&&int.TryParse(args[0],out天))
{
如果(!(天数<1 | |天>366))
{
DateTime日期=新的日期时间(DateTime.Now.Year,1,1).AddDays(天-1);
WriteLine(“日期:{0}”,Date.Day);
WriteLine(“日期:{0}”,Date.Month);
}
}
其他的
{
Console.WriteLine(“输入值不正确或不存在”);
}
}
你可以这样试试
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();
}
static void Main(字符串[]args)
{
int[]arryofdays=新int[]{31,28,31,30,31,30,31,31,30,31};
int num=Int32.Parse(Console.ReadLine());
整数天=0;
整月=0;
对于(int i=0;iarryofdays[i]){
num-=到达日期[i];
月++;
}否则{
天数=num;
打破
}
}
Console.WriteLine(“日期:+days.ToString());
Console.WriteLine(“月:”+(月+1.ToString());
Console.ReadLine();
}
假设num=40
,在for循环的第一次迭代中,它将检查num>arryofdays[0]
,即40>31
。这将返回true,因此31将从num
递减,使num
的值为9<代码>月份将递增。在下一次迭代(i=1
)中,它将检查是否num>arryofdays[1]
,也就是说如果9>28
,这意味着只有9天,我们break
,因为我们不需要再继续了
您的输出将是
日期:9
月份:2
您的代码在一月不起作用的原因是,因为在
temp=temp-arryofdays[i]代码>语句如果inout小于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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text.RegularExpressions;
名称空间测试仪
{
公共课程
{
公共静态void Main(字符串[]args)
{
//你的密码在这里
控制台.WriteLine(“你好,世界!”);
int[]arryofdays=新int[]{31,28,31,30,31,30,31,31,30,31};
int num=Int32.Parse(Console.ReadLine());
int temp=num;
字符串日期,月;
对于(int i=0;i0)
{
Console.WriteLine(“日期:+temp.ToString());
Console.WriteLine(“月:”+(i+2.ToString());
打破
}else{//用于处理第一个月
Console.WriteLine(“日期:+num”);
控制台写入线(“月份:+1”);
打破
}
}
Console.ReadLine();
}
}
}
工作代码:
希望能有所帮助。感谢您提供的类型安全选项。肯定会有帮助的