“mscorlib.dll 中发生了类型为'System.FormatException'的未处理异常”

本文关键字:FormatException 未处理 异常 System mscorlib 类型 发生了 dll | 更新日期: 2023-09-27 18:25:34

    static void Main(string[] args)
    {
        int numVal = -1;
        int numval = -1;
        bool repeat = true;
        Console.WriteLine("Welcome to the perpendicular line finder!");
        while (repeat == true)
        {
            Console.WriteLine("Please write the gradient of the line");

            string userValue = Console.ReadLine();
            try
            {
                numVal = Convert.ToInt32(userValue);
            }
            catch (FormatException e)
            {
                Console.WriteLine("That is not a valid number");
                continue;
            }
            catch (OverflowException e)
            {
                Console.WriteLine("That number is too large, sorry i cannot help you");
                continue;
            }
            Console.WriteLine("So the gradient is {0}? Y/N", numVal);
            string go = Console.ReadLine();
            if (go == "Y" || go == "y")
            {
                repeat = true;
            }
            else
            {
                repeat = false;
            }
            Console.WriteLine("Please write the number that was added or subtracted");
            Console.WriteLine("but if it was subtracted leave the minus sign in");
            string userValue2 = Console.ReadLine();
            try
            {
                numval = Convert.ToInt32(userValue2);
            }
            catch (FormatException e)
            {
                Console.WriteLine("That is not a valid number");
                continue;
            }
            catch (OverflowException e)
            {
                Console.WriteLine("That number is too large, sorry i cannot help you");
                continue;
            }
            Console.WriteLine("So the added or subtracted number is {0}? Y/N", numval);
            string go1 = Console.ReadLine();
            if (go1 == "Y" || go1 == "y")
            {
                repeat = true;
            }
            else
            {
                repeat = false;
            }
            int answer = -1 / numVal; 
            Console.WriteLine("A perpendicular line to y = {0}x+{1} is y = {3}x", numVal, userValue2, answer);   
            Console.ReadLine();
        }

它出现此错误:mscorlib 中发生了类型为"System.FormatException"的未处理异常.dll当我运行代码并转到代码的倒数第二行时,就会发生这种情况。我是 c# 的新手,不知道该怎么做,请帮助我。

“mscorlib.dll 中发生了类型为'System.FormatException'的未处理异常”

Console.WriteLine("A perpendicular line to y = {0}x+{1} is y = {3}x",
    numVal, userValue2, answer);   

格式字符串引用不存在的参数{3},替换为{2}