System.formatexception:输入字符串的格式不正确 [英] System.formatexception: input string was not in a correct format

查看:1384
本文介绍了System.formatexception:输入字符串的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码显示错误

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




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);
          
        }
    }
}





但如果我简单评论下面的代码工作正常:





But if i simple comment the code as below it is working fine:

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());

但它也显示相同的错误。



Please tell me the reason.
Thanks


What I have tried:

int p1 = Convert.ToInt32(Console.ReadLine().Trim());
but it also showing the same error.

推荐答案

您正在输入一个字符并将其读取为int。那就是问题所在。纠正它。
You are inputing a character and reading it as int. That's the problem. Correct it.


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



你可能想要查看Int32.TryParse()。首先阅读文档中的文档。
It's not a problem with the code. The problem is with the input you're trying to feed this line of code. Whatever you're typing at the console cannot be converted to an integer.

You might want to look into Int32.TryParse() instead. Read up on it in the documentation first.


除了解决方案1和2:



不要使用class 转换,当它不是真的需要时。这不是转换;这是解析。更好地使用完全相同的方法,在您的情况下, int.Parse ,或者更好的是,不抛出异常的方法, int.TryParse

https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx



-SA
In addition to the Solutions 1 and 2:

Don't use the class Convert when it's not really needed. This is not "conversion"; this is parsing. Better use the method which does exactly that, in your case, int.Parse, or, better, the method not throwing the exception, int.TryParse:
https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx.

—SA


这篇关于System.formatexception:输入字符串的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆