c语言输入字符串的格式不正确的是,关于c#:输入字符串的格式不正确
我是一個新的C語言,我有一些基本的Java知識,但是我不能讓這個代碼正常運行。
它只是一個基本的計算器,但當我運行程序vs2008時,會出現以下錯誤:
我做了幾乎相同的程序,但在JAVA中使用jSnand,它工作得很好。
這是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 ="";
}
}
有什么問題?有辦法解決嗎?
附:我也試過了
a = Convert.ToInt32(textBox1.text);
b = Convert.ToInt32(textBox2.text);
但它不起作用。
錯誤意味著您試圖從中分析整數的字符串實際上不包含有效的整數。
在創建表單時,文本框不太可能立即包含有效的整數,而這正是您獲取整數值的地方。更新按鈕單擊事件中的a和b會更有意義(與您在構造函數中的方式相同)。另外,檢查Int.TryParse方法——如果字符串實際上不包含整數,那么使用起來就容易多了——它不會引發異常,因此更容易從中恢復。
嘗試轉換時,也可能引發此錯誤消息。若要從具有不同CultureInfo的用戶進行雙輸入混合,則可以使用Convert.ToDouble(String,?而不僅僅是convert.todouble(字符串)。很難調試,因為程序將在您的系統上工作,但會向某些用戶拋出錯誤,這就是為什么我有一個方法在服務器上記錄錯誤,并且我很快發現了這個問題。
我遇到了這個確切的異常,只是它與解析數字輸入無關。所以這不是對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"]);
希望這能幫助其他人找到這個問題,但不分析數字數據。
如果不明確驗證文本字段中的數字,則最好使用
int result=0;
if(int.TryParse(textBox1.Text,out result))
現在,如果結果是成功的,那么您可以繼續進行計算。
通常,不需要初始化result。
問題
出現錯誤的可能原因有:
因為textBox1.Text只包含數字,但數字太大/太小
因為textBox1.Text包含:
a)非數字(除開頭/結尾的space、開頭的-)和/或
b)代碼的應用區域性中的千個分隔符,未指定NumberStyles.AllowThousands或指定NumberStyles.AllowThousands但在區域性中輸入了錯誤的thousand separator和/或
c)十進制分隔符(不應存在于int解析中)
不正常示例:
案例1
a = Int32.Parse("5000000000"); //5 billions, too large
b = 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 beginning
a = 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.AllowThousands
b = 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 beginning
b = 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 culture
b = Int32.Parse("1 189", styles, new CultureInfo("fr-FR")); //ok, having NumberStyles.AllowThousands and correct thousand separator is used for"fr-FR" culture
解決
在所有情況下,請使用Visual Studio調試器檢查textBox1.Text的值,并確保它對于int范圍具有完全可接受的數字格式。像這樣:
1234
另外,你可以考慮
使用TryParse而不是Parse,以確保未解析的數字不會導致異常問題。
檢查EDOCX1[13]的結果,如果不是EDOCX1[16]則處理。
int val;
bool result = int.TryParse(textbox1.Text, out val);
if (!result)
return; //something has gone wrong
//OK, continue using val
在我的情況下,我忘記了放雙花括號逃跑。{{MyObjult}}
您沒有提到您的文本框是否在設計時或現在具有值。當窗體初始化時,如果在窗體設計期間未將文本框放入文本框,則文本框可能不具有值。通過在desgin中設置文本屬性,可以在表單設計中輸入int值,這樣應該可以工作。
我有一個類似的問題,我用以下方法解決了:
在下面的代碼行中引發了異常(請參見下面用**修飾的文本):
static void Main(string[] args)
{
double number = 0;
string numberStr = string.Format("{0:C2}", 100);
**number = Double.Parse(numberStr);**
Console.WriteLine("The number is {0}", number);
}
經過一番調查,我意識到問題在于格式化字符串中包含了一個美元符號($),Parse/Tryparse方法無法解決這個問題(即剝離)。因此,使用字符串對象的remove(…)方法,我將行更改為:
number = Double.Parse(numberStr.Remove(0, 1)); // Remove the"$" from the number
此時,parse(…)方法按預期工作。
這也是我的問題。在我的例子中,我把波斯語號碼改成拉丁語號碼,它起作用了。在轉換之前還要修剪你的繩子。
PersianCalendar pc = new PersianCalendar();
char[] seperator ={'/'};
string[] date = txtSaleDate.Text.Split(seperator);
int a = Convert.ToInt32(Persia.Number.ConvertToLatin(date[0]).Trim());
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的c语言输入字符串的格式不正确的是,关于c#:输入字符串的格式不正确的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux7 重新开始udev,Redh
- 下一篇: 调试一个c语言程序要经过,c语言程序调试