namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{ try
{
string agedesc = GetAgeDsc(300);//这里输入300,就可以正常抛出异常,而我如果输入的是10,则一直停留在黑屏,没有任何提示,正确的应该是提示“青少年”,是下面的if语句那些地方错误了,没有执行到?
}
catch(Exception ex)
{
Console.WriteLine("数据错误:" + ex.Message);
}
Console.ReadKey();
}
static string GetAgeDsc(int age)
{ if(age > 0 && age <= 3)
{
return ("婴幼儿");
}
else if (age > 3 && age <= 18)
{
return ("青少年");
}
else if (age > 19 && age < 150)
{
return ("成年人");
} if (age < 0)
{
throw new Exception("您来自反物质世界吧?");
} else
{
throw new Exception("您见过老佛爷吗?");
} }
}
}
{
class Program
{
static void Main(string[] args)
{ try
{
string agedesc = GetAgeDsc(300);//这里输入300,就可以正常抛出异常,而我如果输入的是10,则一直停留在黑屏,没有任何提示,正确的应该是提示“青少年”,是下面的if语句那些地方错误了,没有执行到?
}
catch(Exception ex)
{
Console.WriteLine("数据错误:" + ex.Message);
}
Console.ReadKey();
}
static string GetAgeDsc(int age)
{ if(age > 0 && age <= 3)
{
return ("婴幼儿");
}
else if (age > 3 && age <= 18)
{
return ("青少年");
}
else if (age > 19 && age < 150)
{
return ("成年人");
} if (age < 0)
{
throw new Exception("您来自反物质世界吧?");
} else
{
throw new Exception("您见过老佛爷吗?");
} }
}
}
然后你代码后面只有一个Console.ReadKey();
那你说它会不会一直黑屏!
else
{
throw new Exception("您见过老佛爷吗?");
}
当输入10时候,返回agedesc 就是 青少年,下面的就不执行了,而你没有显示“青少年”语句希望对你有帮助……
static string GetAgeDsc(int age)
{
if (age > 0 && age <= 3)
{
return ("婴幼儿");
}
else if (age > 3 && age <= 18)
{
return ("青少年");
}
else if (age > 19 && age < 150)
{
return ("成年人");
}
else if (age < 0)
{
throw new Exception("您来自反物质世界吧?");
}
else
{
throw new Exception("您见过老佛爷吗?");
}
}
static string GetAgeDsc(int age)
{ if (age > 0 && age <= 3)
{
return ("婴幼儿");
}
if (age > 3 && age <= 18)
{
return ("青少年");
}
if (age > 19 && age < 150)
{
return ("成年人");
} if (age < 0)
{
throw new Exception("您来自反物质世界吧?");
} throw new Exception("您见过老佛爷吗?");
}