如何编写一个循环来打印此类有关日期差异的文本?

发布于 2024-12-10 15:33:57 字数 250 浏览 0 评论 0原文

我需要生成一个代码,在其中我将获取用户输入(int)并使用它我需要生成一周中的几天

示例:用户输入 - 5

我需要输出如下内容

  1. 今天是(今天日期)
  2. 明天是(明天日期)
  3. 后天是后
  4. 两天,
  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

  1. Today is (todays date)
  2. Tomorrow is (tomorrows date)
  3. Day after tomorrow is
  4. two days after tomorrow is
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

奈何桥上唱咆哮 2024-12-17 15:33:57

我不会为你写代码。想给一些提示。

您可以使用 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

请恋爱 2024-12-17 15:33:57
 static void Main(string[] args)
        {
            days();
        }
        public static void days()
        {
            Console.Write("Please enter number : ");
            int a = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < a; i++)
            {
                string strdays;
                switch (i)
                {
                    case 0:
                        strdays = ". Todays date is : ";
                        break;
                    case 1:
                        strdays = ". Tommorrows date is : ";
                        break;
                    case 2:
                        strdays = ". Day after tommorrows date is : ";
                        break;                   
                    default:
                        strdays = ". " + (i-1) + "Days after tommorrows date is : ";
                        break;
                }                    
                Console.WriteLine((i+1)+ strdays + System.DateTime.Now.AddDays(i));                 
            }            
            Console.ReadLine();
        }

希望这可以帮助你..

 static void Main(string[] args)
        {
            days();
        }
        public static void days()
        {
            Console.Write("Please enter number : ");
            int a = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < a; i++)
            {
                string strdays;
                switch (i)
                {
                    case 0:
                        strdays = ". Todays date is : ";
                        break;
                    case 1:
                        strdays = ". Tommorrows date is : ";
                        break;
                    case 2:
                        strdays = ". Day after tommorrows date is : ";
                        break;                   
                    default:
                        strdays = ". " + (i-1) + "Days after tommorrows date is : ";
                        break;
                }                    
                Console.WriteLine((i+1)+ strdays + System.DateTime.Now.AddDays(i));                 
            }            
            Console.ReadLine();
        }

hope this help u..

南烟 2024-12-17 15:33:57

您需要的代码片段:

  • Console.Write
  • Console.ReadLine
  • int.Parse (将输入转换为整数)
  • DateTime.Today
  • DateTime.AddDays( ZeroOrMoreDays)
  • DateTime.DayOfWeek (如果您想要 Wednesday 而不是 10/19/2011 12:00:00 AM

这是一些伪代码:

Ask the user for the number of days (as a string)
Parse numberOfDays into an int
for i = 0 to numberOfDays:
    write line: today.AddDays(i).DayOfWeek

您还需要执行一些技巧才能使自然语言的内容正常工作,例如明天后三天

我会使用一些额外的 if i == 0 else if i == 1 类型语句来解决这个问题,然后依靠通用的 ;后天 在某个时间点之后。

请参阅此(已关闭)问题,了解有关如何获取该数字的链接:将数字转换为to Words C#

根据您对问题的评论进行编辑

@ sblom ...不适合我正在计划的网络服务

不要使用Console东西。

将此作为一个方法,并且不要执行 string/int.Parse 的操作。直接取一个int就可以了。

使用StringBuilder构建结果,并返回字符串,而不是直接打印结果。

The pieces of code you will need:

  • Console.Write
  • Console.ReadLine
  • int.Parse (to convert the input to an integer)
  • DateTime.Today
  • DateTime.AddDays(zeroOrMoreDays)
  • DateTime.DayOfWeek (if you want Wednesday instead of 10/19/2011 12:00:00 AM)

Here is some pseudo-code:

Ask the user for the number of days (as a string)
Parse numberOfDays into an int
for i = 0 to numberOfDays:
    write line: today.AddDays(i).DayOfWeek

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

@ sblom ... nope for a web service i'm planning

Don't use Console stuff then.

Make this a method instead, and don't do the string/int.Parse stuff. Just take an int directly.

Build a result with a StringBuilder, and return a string, rather than printing out the result directly.

南渊 2024-12-17 15:33:57
  int diff=0;
  while (diff < 5)
  {
      Console.WriteLine(DateTime.Now.AddDays(diff));
      diff++;
  }
  int diff=0;
  while (diff < 5)
  {
      Console.WriteLine(DateTime.Now.AddDays(diff));
      diff++;
  }
一梦等七年七年为一梦 2024-12-17 15:33:57
Int32 numberOfDays = 5;
DateTime dt = DateTime.Now;
for (int i = 0; i < numberOfDays ; i++)
{
    Console.WriteLine(dt.AddDays(i).DayOfWeek);
}

希望有帮助! =)

Int32 numberOfDays = 5;
DateTime dt = DateTime.Now;
for (int i = 0; i < numberOfDays ; i++)
{
    Console.WriteLine(dt.AddDays(i).DayOfWeek);
}

Hope it helps! =)

拥抱没勇气 2024-12-17 15:33:57
// num is number if days we'll loop through
int num = 5;

// gets the current date time - save it off.  even though incrementing days, in other
// date incrementing functions (min, sec etc...) it's important to save off
DateTime now = DateTime.Now;

// init a countervar
int count = 0;

// loop until num times
while (count < num)
{
    // add the current count number of days and print
    // ++ after the variable increments it after calculating the new date
    DateTime curr = now.AddDays(count++);

    // output the value - it ends up calling .ToString() on date.
    Console.WriteLine(curr);
}
// num is number if days we'll loop through
int num = 5;

// gets the current date time - save it off.  even though incrementing days, in other
// date incrementing functions (min, sec etc...) it's important to save off
DateTime now = DateTime.Now;

// init a countervar
int count = 0;

// loop until num times
while (count < num)
{
    // add the current count number of days and print
    // ++ after the variable increments it after calculating the new date
    DateTime curr = now.AddDays(count++);

    // output the value - it ends up calling .ToString() on date.
    Console.WriteLine(curr);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文