-3

我只是想知道这是否可能:

int custom = Console.ReadLine(); //or string custom?
Console.WriteLine(DateTime.Now.Add(custom);

我知道这Console.ReadLine()等于string。但尽管如此,我确信这在某种程度上是可能的。谷歌帮不了我,所以我想我会是第一个问的。(或者我只是不擅长搜索)

4

1 回答 1

4

ReadLine()您可以将结果解析为int

int custom = 0;
if(int.TryParse(Console.ReadLine(), out custom)) {
  Console.WriteLine(DateTime.Now.AddHours(custom));
}
else {
  Console.WriteLine("Invalid number!");
}
于 2013-10-02T10:38:54.803 回答