【发布时间】:2014-08-04 10:00:39
【问题描述】:
我遇到了这个非常烦人的问题(我知道这是基本的东西)但是当我尝试使用 tryparse 时,我必须在它说整数之前输入 2 个值,我希望它在 1 次尝试后说整数。 (顺便说一句,我必须使用 tryparse) 这是一个例子。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int results = 0;
Console.WriteLine("how old are you?");
int.TryParse (Console.ReadLine(), out results);
if (int.TryParse (Console.ReadLine(), out results))
{
Console.WriteLine("integer");
}
else
{
Console.WriteLine("not an integer");
}
Console.ReadLine();
}
}
}
【问题讨论】:
-
使用变量
string input = Console.ReadLine(); -
你的代码完全按照你的要求去做。