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("您见过老佛爷吗?");
            }        }
    }
}

解决方案 »

  1.   

    没问题啊, agedesc 就是 青少年,如果还有问题,你自己调试,这种问题你有必要问吗
      

  2.   

    我想知道 你输入10的时候 GetAgeDsc通过 然后得到青少年
    然后你代码后面只有一个Console.ReadKey();
    那你说它会不会一直黑屏!
      

  3.   

    除了捕获异常中,其他根本就没有Console.WriteLine(agedesc),能不黑屏吗???我的眼睛啊
      

  4.   

    当你输入300时前面的if/else if 都不满足条件,所以执行
    else
      {
      throw new Exception("您见过老佛爷吗?");
      }
    当输入10时候,返回agedesc 就是 青少年,下面的就不执行了,而你没有显示“青少年”语句希望对你有帮助……
      

  5.   


    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("您见过老佛爷吗?");
                }
            }
      

  6.   

    if里只是return了结果(不是异常),你前台并没有将函数返回值输出(Console.WriteLine)
      

  7.   

    蛋疼的程序……
            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("您见过老佛爷吗?");
            }