简体   繁体   English

未处理的异常:System.FormatException

[英]Unhandled Exception: System.FormatException

I'm having a trouble trying to fix a simple c# code. 我在尝试修复一个简单的c#代码时遇到了麻烦。 Here's the code: 这是代码:

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

namespace MoonGravity
{
    class MoonGravity
    {
        static void Main(string[] args)
        {
            int number = Convert.ToInt32(Console.ReadLine());
            float gravity = (number * 0.17f);
            Console.WriteLine(gravity.ToString("F3"));
        }
    }
}

I need it to output a single floating-point value and all values must be with exactly 3-digit precision after the floating point. 我需要它输出一个浮点值,并且浮点后所有值必须精确到3位精度。 I got it to work, the only problem is that it crashes if I don't input a whole number. 我得到了它的工作,唯一的问题是,如果我不输入整数,它会崩溃。 That's the error i get. 这就是我得到的错误。

Unhandled Exception: System.FormatException: Input string was not in a correct format. 未处理的异常:System.FormatException:输入字符串的格式不正确。 at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at System.Convert.ToInt32(String value) at MoonGravity.MoonGravity.Main(String[] args) in .... 2015\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Program.cs:line 13 System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer&number,NumberFormatInfo info,Boolean parseDecimal)at System.Number.ParseInt32(String s,NumberStyles style,NumberFormatInfo info)at System.Convert.ToInt32(String value)at MoonGravity .MoonGravity.Main(String [] args)in .... 2015 \\ Projects \\ ConsoleApplication3 \\ ConsoleApplication3 \\ Program.cs:第13行

I know the problem comes from my Convert.ToInt32 and I tried to fix it but i couldn't. 我知道问题来自我的Convert.ToInt32 ,我试图解决它,但我不能。 Thank you for the help. 感谢您的帮助。

If you want to be able to input non-whole numbers, perform a different conversion. 如果您希望能够输入非整数,请执行不同的转换。 There are also methods provided in .NET that will try to parse input into another type and give you boolean return for if it succeeded, instead of throwing an exception. .NET中还提供了一些方法,它们会尝试将输入解析为另一种类型,如果成功则给出布尔返回值,而不是抛出异常。 Using try parse is especially handy when bringing in user input and you have no idea what they may try to give you. 使用try parse在引入用户输入时尤其方便,而您不知道他们可能会尝试给您什么。 You can then gracefully handle the likely condition that incorrect or invalid input is given, instead of catching a format exception, etc. MSDN Single Try Parse 然后,您可以优雅地处理给出错误或无效输入的可能条件,而不是捕获格式异常等.MSDN Single Try Parse

static void Main(string[] args)
    {
        float number;
        var success = Single.TryParse(Console.ReadLine(), number);
        if(success)
        {
            float gravity = (number * 0.17f);
            Console.WriteLine(gravity.ToString("F3"));
        }
        else
        {
            Console.WriteLine("Only numbers allowed.");
        }
    }

That's because your number is int . 那是因为你的numberint Try changing it to float . 尝试将其更改为float

float number = (float)Convert.ToDouble(Console.ReadLine());
float gravity = (number * 0.17f);
Console.WriteLine(gravity.ToString("F3"));

Below line is incorrect: 下面的行不正确:

int number = Convert.ToInt32(Console.ReadLine());

An integer (from the Latin integer meaning "whole") is a number that can be written without a fractional component. 整数(来自拉丁整数,意思是“整数”)是可以在没有小数分量的情况下写入的数字。 For example, 21, 4, 0, and −2048 are integers, while 9.75, 5 1⁄2, and √2 are not. 例如,21,4,0​​和-2048是整数,而9.75,5 1/2和√2不是。

change it to decimal, double or float 将其更改为十进制,双精度或浮点数

decimal number = Convert.ToDecimal(Console.ReadLine());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型为“ System.FormatException”的未处理异常 - An unhandled exception of type 'System.FormatException' 发生类型为'System.FormatException'的未处理的异常 - An unhandled exception of type 'System.FormatException' occurred mscorlib.dll中发生了'System.FormatException'类型的未处理异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll 类型为“ System.FormatException”的未处理的异常输入字符串的格式不正确 - Unhandled exception of type “System.FormatException” Input string was not in correct format 退出应用程序后,类型为'System.FormatException'的未处理异常 - An unhandled exception of type 'System.FormatException' after exiting the app “ mscorlib.dll中发生了'System.FormatException'类型的未处理的异常” - “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll” 未处理的异常:System.FormatException:输入字符串的格式不正确 - Unhandled Exception: System.FormatException: Input string was not in a correct format mscorlib.dll中发生了未处理的“System.FormatException”类型异常 - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll 错误:未处理的异常:System.FormatException:输入字符串的格式不正确 - Error: Unhandled Exception: System.FormatException: Input string was not in a correct format System.FormatException 未由用户代码处理 - System.FormatException is unhandled by user code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM