为了账号安全,请及时绑定邮箱和手机立即绑定

输入字符串格式不正确。

输入字符串格式不正确。

C#
一只斗牛犬 2019-06-28 15:02:48
输入字符串格式不正确。我是C#的新手,我对Java有一些基本知识,但我无法正确地运行这些代码。这只是一个基本的计算器,但当我运行VS 2008程序时,会出现以下错误:我做了几乎相同的程序,但在java中使用JSwing,它工作得很好。以下是c#的形式:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace calculadorac{     public partial class Form1 : Form     {     int a, b, c;     String resultado;     public Form1()     {         InitializeComponent();         a = Int32.Parse(textBox1.Text);         b = Int32.Parse(textBox2.Text);     }     private void button1_Click(object sender, EventArgs e)     {         add();         result();     }     private void button2_Click(object sender, EventArgs e)     {         substract();         result();     }     private void button3_Click(object sender, EventArgs e)     {         clear();     }     private void add()     {         c = a + b;         resultado = Convert.ToString(c);     }     private void substract()     {         c = a - b;         resultado = Convert.ToString(c);     }     private void result()     {         label1.Text = resultado;     }     private void clear()     {         label1.Text = "";         textBox1.Text = "";         textBox2.Text = "";     }}有什么问题吗?有办法解决吗?PS:我也试过a = Convert.ToInt32(textBox1.text);b = Convert.ToInt32(textBox2.text);但没起作用。
查看完整描述

3 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

我遇到了这个确切的异常,只是它与解析数字输入无关。所以这不是OP问题的答案,但我认为分享知识是可以接受的。

我声明了一个字符串,并将其格式化,以便与JQTree它需要花括号({})。您必须使用双大括号才能将其接受为格式正确的字符串:

string measurements = string.empty;measurements += string.Format(@"
    {{label: 'Measurement Name: {0}',
        children: [
            {{label: 'Measured Value: {1}'}},
            {{label: 'Min: {2}'}},
            {{label: 'Max: {3}'}},
            {{label: 'Measured String: {4}'}},
            {{label: 'Expected String: {5}'}},
        ]
    }},",
    drv["MeasurementName"] == null ? "NULL" : drv["MeasurementName"],
    drv["MeasuredValue"] == null ? "NULL" : drv["MeasuredValue"],
    drv["Min"] == null ? "NULL" : drv["Min"],
    drv["Max"] == null ? "NULL" : drv["Max"],
    drv["MeasuredString"] == null ? "NULL" : drv["MeasuredString"],
    drv["ExpectedString"] == null ? "NULL" : drv["ExpectedString"]);

希望这将有助于发现这个问题但不解析数值数据的其他人。


查看完整回答
反对 回复 2019-06-28
?
胡说叔叔

TA贡献1804条经验 获得超8个赞

问题

有一些可能发生错误的情况:

  1. 因为textBox1.Text只包含数字,但数字是太大/太小

  2. 因为textBox1.Text包含:

    • (A)非数字(除外)

      space

      在开始/结束时,

      -

      (一开始)和/或
    • (B)您的代码的应用区域性中有1000个分隔符,而没有指定

      NumberStyles.AllowThousands

      或者您指定

      NumberStyles.AllowThousands

      但错了

      thousand separator

      在文化和/或
    • (C)十进制分隔符(不应存在于

      int

      分析)

不确定的例子:

案例1

a = Int32.Parse("5000000000"); //5 billions, too largeb = Int32.Parse("-5000000000"); //-5 billions, too small//The limit for int (32-bit integer) is only from -2,147,483,648 to 2,147,483,647

案例2 a)

a = Int32.Parse("a189"); //having a a = Int32.Parse("1-89"); //having - but not in the beginninga = Int32.Parse("18 9"); //having space, but not in the beginning or end

案件2 b)

NumberStyles styles = NumberStyles.AllowThousands;a = Int32.Parse("1,189"); //not OK, no NumberStyles.AllowThousandsb = Int32.Parse("1,189", styles, new CultureInfo("fr-FR")); //not OK, having NumberStyles.AllowThousands but the culture specified use different thousand separator

案件2 c)

NumberStyles styles = NumberStyles.AllowDecimalPoint;a = Int32.Parse("1.189", styles); //wrong, int parse cannot parse decimal point at all!

似乎不确定,但实际上是好的例子:

案例2 a)OK

a = Int32.Parse("-189"); //having - but in the beginningb = Int32.Parse(" 189 "); //having space, but in the beginning or end

案例2 b)OK

NumberStyles styles = NumberStyles.AllowThousands;a = Int32.Parse("1,189", styles); //ok, having NumberStyles.AllowThousands in the correct cultureb = Int32.Parse("1 189", styles, new CultureInfo("fr-FR")); //ok, having NumberStyles.AllowThousands and correct thousand separator is used for "fr-FR" culture

在所有情况下,请检查textBox1.Text使用VisualStudio调试器,并确保它具有完全可以接受的数字格式。int范围。就像这样:

1234

此外,你也可以考虑

  1. 使用

    TryParse

    而不是

    Parse

    若要确保非解析数字不会导致异常问题,请执行以下操作。
  2. 检查…的结果TryParse如果不处理的话true

    int val;bool result = int.TryParse(textbox1.Text, out val);if (!result)
        return; //something has gone wrong//OK, continue using val


查看完整回答
反对 回复 2019-06-28
  • 3 回答
  • 0 关注
  • 1036 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信