在第16行抛出异常

时间:2018-06-09 08:30:44

标签: c# unity3d

using System;

namespace Rndom
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            start:
            Random obj = new Random();
            int num1 = obj.Next(1, 9);
            int num2 = obj.Next(10,21);
            Console.WriteLine("sum of two unique numbers\n"+num1+" "+num2+"  "+(num1+num2));
            char ch;
            Console.WriteLine("If wannt to continue, press y");
            ch = Convert.ToChar(Console.ReadKey()); // <- I have exception thrown here
            if(ch=='y')
            {
                goto start;
            }
            else
            {
                Console.WriteLine("press any key to terminate");
                Console.ReadKey();
            }

        }

    }
}

1 个答案:

答案 0 :(得分:2)

而不是

ch = Convert.ToChar(Console.ReadKey());

你应该把它作为

ch = Console.ReadKey().KeyChar;

ConsoleKeyInfo返回的Console.ReadKey()实例无法转换char