C#运行时错误

C#运行时错误,c#,C#,有谁能告诉我为什么这个程序会出现运行时错误: Unhandled Exception: System.FormatException: Input string was not in the correct format at System.Int64.Parse (System.String s) [0x00000] in <filename unknown>:0 at Myclass.Main (System.String[] args) [0x00000] in &l

有谁能告诉我为什么这个程序会出现运行时错误:

Unhandled Exception: System.FormatException: Input string was not in the correct format
  at System.Int64.Parse (System.String s) [0x00000] in <filename unknown>:0 
  at Myclass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.FormatException: Input string was not in the correct format
  at System.Int64.Parse (System.String s) [0x00000] in <filename unknown>:0 
  at Myclass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
和输入:

2
1 2
请参阅以下内容后的。

Console.ReadLine().Trim() 

您的
res
值为
“12”
,它的长度肯定不正确。

在正常情况下,您的代码可以正常工作,但如果您键入除数字以外的字符,它将给出运行时错误,因为将
字符串
转换为
长度

可以使用TryParse()方法代替Parse():

编辑后:
Parse()
方法无法将“12”转换为
long
,它在数字之间包含
空格

        long t = 0;
        bool test = Int64.TryParse(p, out t);

        if(!test)
        {
           Console.WriteLine("Wrong String format");
           return;
        }

        while (t > 0)
        {
            //do stuff
        }

这样写吧,你正试图解析为不长的长字符串。因此你有这个例外。如果您这样写,您将首先检查是否可能,如果转换为long成功,您的值将写入t,如果不
test
return false,您将显示错误消息。

更改代码以处理用户输入的字符串,这些字符串不能正确解析为Int64,如果发生错误,则报告错误

        var test = Console.ReadLine().Trim();
        long t;
        if (Int64.TryParse(test, out t))
        {
            while (t > 0)
            {
                t--;
                var res = Console.ReadLine().Trim();
                var n = Int64.Parse(res);
                Console.WriteLine(n);
                var ans = n*8;
                if (n > 1)
                {
                    ans = ans + (n - 1)*6;
                }
                Console.WriteLine(ans);
                Console.WriteLine("\n");
            }
        }
        else
        {
            Console.WriteLine("Invalid argument {0} entered.", test);
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();

究竟什么是例外?例如,代码中的
Int64.Parse(test)
将引发
FormatException
是您将输入字母而不是数字。您不理解“输入字符串格式不正确”的哪一部分?
        long t = 0;
        bool test = Int64.TryParse(p, out t);

        if(!test)
        {
           Console.WriteLine("Wrong String format");
           return;
        }

        while (t > 0)
        {
            //do stuff
        }
        var test = Console.ReadLine().Trim();
        long t;
        if (Int64.TryParse(test, out t))
        {
            while (t > 0)
            {
                t--;
                var res = Console.ReadLine().Trim();
                var n = Int64.Parse(res);
                Console.WriteLine(n);
                var ans = n*8;
                if (n > 1)
                {
                    ans = ans + (n - 1)*6;
                }
                Console.WriteLine(ans);
                Console.WriteLine("\n");
            }
        }
        else
        {
            Console.WriteLine("Invalid argument {0} entered.", test);
        }

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();