111,131
社区成员
发帖
与我相关
我的任务
分享
if(x<0)
{
y=-1;
}
else if(x=0)
{
y=0;
}
else
{
y=1;
}
static void Main(string[] args)
{
Console.Write("请输入X的值:");
string data = Console.ReadLine();
bool flag = IsNumber(data);
if (flag == true)
{
int intData = Convert.ToInt32(data);
int Y;
if (intData > 0)
{
Y = 1;
}
else
{
if (intData == 0)
{
Y = 0;
}
else
{
Y = -1;
}
}
Console.WriteLine("根据您输入的X值,得出Y的值为:"+Y.ToString());
}
else
{
Console.WriteLine("请输入整数");
}
Console.ReadKey();
}
static bool IsNumber(string data)
{
Regex r = new Regex("^[-]{0,1}\\d+$");//这个正则只验证整数,根据需要,自己写正则
Match m = r.Match(data);
if (m.Success)
{
return true;
}
return false;
}