如何编写一个循环来打印此类有关日期差异的文本?
我需要生成一个代码,在其中我将获取用户输入(int)并使用它我需要生成一周中的几天
示例:用户输入 - 5
我需要输出如下内容
- 今天是(今天日期)
- 明天是(明天日期)
- 后天是后
- 两天,
- 明天是三天后……
如果用户输入 6,则应输出如上所示的 6 天。
请帮忙,因为我是 C# 新手,
非常感谢
I need to generate a code where i will be getting an user input (an int) and using that i need to generate days of the week
example: user inputs - 5
I need to output something like below
- Today is (todays date)
- Tomorrow is (tomorrows date)
- Day after tomorrow is
- two days after tomorrow is
- three days after tomorrow is ...
and if the user enters 6 it should output 6days as above.
please help as i'm an novice in C#
Thanks alot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不会为你写代码。想给一些提示。
您可以使用 DateTime 类来查找当前日期和时间,它还提供了可以轻松用于查找第二天和类似日期的方法。
您可以按照标题中所述使用 While 循环,甚至 For 也可以工作。
如果您正在制作基于控制台的应用程序,则可以通过 Console 类从用户那里获取输入,否则您可以在某些文本框中获取输入。
For 循环或 while 循环将在当天运行加上用户输入的数字减 1
I won't write code for you. Would like to give some hints.
You can use DateTime class to find out the current date and time and it also provide methods which you can easily use to find what next day is and similar.
You can use While Loop as you said in Title or even For can work
You can take input from user through Console class if you are making console based app otherwise you can take input in some textbox.
For loop or while loop will run for current day plus number that user have entered minus 1
希望这可以帮助你..
hope this help u..
您需要的代码片段:
int.Parse
(将输入转换为整数)DateTime.Today
DateTime.AddDays( ZeroOrMoreDays)
DateTime.DayOfWeek
(如果您想要Wednesday
而不是10/19/2011 12:00:00 AM
)这是一些伪代码:
您还需要执行一些技巧才能使自然语言的内容正常工作,例如
明天后三天
。我会使用一些额外的
if i == 0
else if i == 1
类型语句来解决这个问题,然后依靠通用的;后天
在某个时间点之后。请参阅此(已关闭)问题,了解有关如何获取该数字的链接:将数字转换为to Words C#
根据您对问题的评论进行编辑
不要使用
Console
东西。将此作为一个方法,并且不要执行 string/
int.Parse
的操作。直接取一个int
就可以了。使用
StringBuilder
构建结果,并返回字符串
,而不是直接打印结果。The pieces of code you will need:
int.Parse
(to convert the input to an integer)DateTime.Today
DateTime.AddDays(zeroOrMoreDays)
DateTime.DayOfWeek
(if you wantWednesday
instead of10/19/2011 12:00:00 AM
)Here is some pseudo-code:
You'll also need to do some tricks to get the natural language stuff to work, e.g.
three days after tomorrow is
.I'd use a few extra
if i == 0
else if i == 1
type statements to solve this, and fall back on a general<number> days after tomorrow
after a certain point.See this (closed) question for links on how to get that number: converting numbers in to words C#
Edit due to your comments on the question
Don't use
Console
stuff then.Make this a method instead, and don't do the string/
int.Parse
stuff. Just take anint
directly.Build a result with a
StringBuilder
, and return astring
, rather than printing out the result directly.希望有帮助! =)
Hope it helps! =)