System.formatException:输入字符串的格式不正确


这段代码显示错误

C#
System.FormatException: Input string was not in a correct format

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            Console.WriteLine("enter the character");
            int c = Console.Read();
            Console.WriteLine(c);

            Console.WriteLine("please enter first integer");
            int p1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("second");
            int p2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(p1 + p2);
          
        }
    }
}

但如果我简单地注释下面的代码,它就可以正常工作:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("hello");
            //Console.WriteLine("enter the character");
            //int c = Console.Read();
            //Console.WriteLine(c);

            Console.WriteLine("please enter first integer");
            int p1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("second");
            int p2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(p1 + p2);
          
        }
    }
}

请告诉我原因。
谢谢

我尝试过的:

int p1 = Convert.ToInt32(Console.ReadLine().Trim());
但它也显示相同的错误。

解决方案1

您正在输入一个字符并将其读取为 int。 那就是问题所在。 纠正它。

解决方案2

这不是代码的问题。 问题在于您尝试为这行代码提供的输入。 无论您在控制台输入什么,都无法转换为整数。

您可能想查看 Int32.TryParse() 。 首先阅读文档中的内容。

解决方案3

除了解决方案 1 和 2 之外:

不要使用该类 Convert 当它不是真正需要的时候。 这不是“转变”,而是“转变”。 这是 解析。 最好使用在您的情况下完全执行此操作的方法, int.Parse,或者更好的是,该方法不抛出异常, int.TryParse

-SA

解决方案4

我复制了你的代码并进行了测试。
即使我输入 integer 它给出的值相同 format exception..
考虑 解决方案1,2,3 有关信息 解析整数值

问题是,你正在使用 Console.Read() 方法

引用:

当您键入输入字符时,Read 方法会阻止其返回; 当您按 Enter 键时,它会终止。 按 Enter 键会将与平台相关的行终止序列附加到您的输入中(例如,Windows 会附加回车换行序列)。 对 Read 方法的后续调用一次检索一个字符。 检索到最后一个字符后,Read 再次阻止其返回并重复该循环。

参考 Console.Read 方法(系统)[^]

尝试调试下面的代码

C#
class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("hello");
            Console.WriteLine("enter the character");
            int c = Console.Read();
            Console.WriteLine(c);

            Console.WriteLine("please enter first integer");
            string p1str = Console.ReadLine(); // this will be ignored if you use Console.Read()
            int p1 = Convert.ToInt32(p1str);
            Console.WriteLine("second");
            string p2str = Console.ReadLine();
            int p2 = Convert.ToInt32(p2str);
            Console.WriteLine(p1 + p2);

        }
    }

Console.Read() 将分配该行中的最后一个字符值。
以及立即的 Console.ReadLine() 用户输入将被忽略,并分配已在屏幕上键入的值。
例如:
如果你输入“a1“那么 p1str 将保持价值“1” 忽略第一个字符
如果你输入“123“那么 p1str 将保持价值“23” 无一例外都可以解析为整数。
如果你输入“1[pressed enter key]“ 然后 p1str 将保存值“”(空或返回键),这将导致 format exception..

我希望你理解,应用断点和调试器,见证它是如何工作的。

解决方案5

Console.WriteLine("hello");
            Console.WriteLine("enter the character");
            int c = Console.Read();
            Console.WriteLine(c);
            Console.ReadLine(); //this work fine
            Console.WriteLine("please enter first integer");
            int p1 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("second");
            int p2 = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(p1 + p2);
        }

コメント

タイトルとURLをコピーしました