输入字符串格式不正确。

输入字符串格式不正确。

我是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);

但没起作用。


一只斗牛犬
浏览 1037回答 3
3回答

千巷猫影

我遇到了这个确切的异常,只是它与解析数字输入无关。所以这不是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"]);希望这将有助于发现这个问题但不解析数值数据的其他人。

胡说叔叔

问题有一些可能发生错误的情况:因为textBox1.Text只包含数字,但数字是太大/太小因为textBox1.Text包含:(A)非数字(除外)space在开始/结束时,-(一开始)和/或(B)您的代码的应用区域性中有1000个分隔符,而没有指定NumberStyles.AllowThousands或者您指定NumberStyles.AllowThousands但错了thousand separator在文化和/或(C)十进制分隔符(不应存在于int分析)不确定的例子:案例1a = 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)OKa = Int32.Parse("-189"); //having - but in the beginningb = Int32.Parse(" 189 "); //having space, but in the beginning or end案例2 b)OKNumberStyles 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此外,你也可以考虑使用TryParse而不是Parse若要确保非解析数字不会导致异常问题,请执行以下操作。检查…的结果TryParse如果不处理的话trueint val;bool result = int.TryParse(textbox1.Text, out val);if (!result)     return; //something has gone wrong//OK, continue using val
打开App,查看更多内容
随时随地看视频慕课网APP