输入字符串格式不正确。我是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个赞
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"]);
胡说叔叔
TA贡献1804条经验 获得超8个赞
问题
因为 textBox1.Text
只包含数字,但数字是 太大/太小 因为 textBox1.Text
包含: (A)非数字(除外) space
在开始/结束时, -
(一开始)和/或 (B)您的代码的应用区域性中有1000个分隔符,而没有指定 NumberStyles.AllowThousands
或者您指定 NumberStyles.AllowThousands
但错了 thousand separator
在文化和/或 (C)十进制分隔符(不应存在于 int
分析)
不确定的例子:
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
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
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
NumberStyles styles = NumberStyles.AllowDecimalPoint;a = Int32.Parse("1.189", styles); //wrong, int parse cannot parse decimal point at all!
似乎不确定,但实际上是好的例子:
a = Int32.Parse("-189"); //having - but in the beginningb = Int32.Parse(" 189 "); //having space, but in the beginning or end
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
int
1234
使用 TryParse
而不是 Parse
若要确保非解析数字不会导致异常问题,请执行以下操作。 检查…的结果 TryParse
如果不处理的话 true
int val;bool result = int.TryParse(textbox1.Text, out val);if (!result) return; //something has gone wrong//OK, continue using val
- 3 回答
- 0 关注
- 1036 浏览
添加回答
举报
0/150
提交
取消