【问题标题】:"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"“在 mscorlib.dll 中发生了 'System.FormatException' 类型的未处理异常”
【发布时间】:2014-10-05 19:50:24
【问题描述】:
    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.dll 中出现“System.FormatException”类型的未处理异常 当我运行代码并进入倒数第二行代码时,就会发生这种情况。 我是 c# 新手,不知道该怎么做,请帮助我。

【问题讨论】:

  • 抱歉,本网站的章程不适用于牲畜。
  • 阅读错误信息,它会告诉你究竟出了什么问题。

标签: c#


【解决方案1】:
Console.WriteLine("A perpendicular line to y = {0}x+{1} is y = {3}x",
    numVal, userValue2, answer);   

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

【讨论】:

  • 谢谢,但它给出的答案总是“y = 0x”,你能解释一下原因吗?
  • @Piggy int answer = -1 / numVal; 看起来不对。应该类似于double answer = -1.0 / numVal;(注意1.0)。整数除法切小数部分。
  • 谢谢,我意识到 -1 是错误的,但我现在不知道如何更改它。
  • @Piggy 或者,您可以使用-1 / (double)numValnumVal 不会是问题,而是 double
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多