【问题标题】:(Error: System.Format Exception has been thrown. "Input was not in correct format") [duplicate](错误:已引发 System.Format 异常。“输入格式不正确”)[重复]
【发布时间】:2019-05-01 23:21:32
【问题描述】:

应用程序正在运行,我在程序中有两个类,但我的第二个类不断收到此错误。

class SandwichBuild 
{
    private string sandwichname;
    private double ingred1, ingred2, ingred3, ingred4;

    public SandwichBuild(string input1, string input2, string input3, string input4, string input5)   //sets variables
    {
        sandwichname = input1;
        ingred1 = double.Parse(input2);
        ingred2 = double.Parse(input3);
        ingred3 = double.Parse(input4);
        ingred4 = double.Parse(input5);
    }

    public void PrintOutput()
    {
        WriteLine(sandwichname + " " + ingred1 + " " + ingred2 + " " + ingred3 + " " + ingred4);  //Output for users responses
    }
}

我需要我的应用程序来显示最终响应

【问题讨论】:

  • 你在哪一行得到了错误?
  • 我在第 7 行收到了错误。
  • ingred1 = double.Parse(input2);
  • 你在input2传递的是什么?
  • 你传递给类构造函数的是什么? WriteLine() 是什么?

标签: c# error-handling


【解决方案1】:

如果您将不是double 的字符串传递给double.Parse(...),则会引发异常。

此代码将引发该异常。

 var result = double.Parse("Not a double or even a number.");

因此,您将一个无效字符串传递给您的构造函数。

【讨论】:

    猜你喜欢
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多