第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

輸入字符串格式不正確。

輸入字符串格式不正確。

C#
一只斗牛犬 2019-06-28 15:02:48
輸入字符串格式不正確。我是C#的新手,我對Java有一些基本知識(shí),但我無法正確地運(yùn)行這些代碼。這只是一個(gè)基本的計(jì)算器,但當(dāng)我運(yùn)行VS 2008程序時(shí),會(huì)出現(xiàn)以下錯(cuò)誤:我做了幾乎相同的程序,但在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貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊

我遇到了這個(gè)確切的異常,只是它與解析數(shù)字輸入無關(guān)。所以這不是OP問題的答案,但我認(rèn)為分享知識(shí)是可以接受的。

我聲明了一個(gè)字符串,并將其格式化,以便與JQTree它需要花括號(hào)({})。您必須使用雙大括號(hào)才能將其接受為格式正確的字符串:

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"]);

希望這將有助于發(fā)現(xiàn)這個(gè)問題但不解析數(shù)值數(shù)據(jù)的其他人。


查看完整回答
反對 回復(fù) 2019-06-28
?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊

問題

有一些可能發(fā)生錯(cuò)誤的情況:

  1. 因?yàn)?/trans>textBox1.Text只包含數(shù)字,但數(shù)字是太大/太小

  2. 因?yàn)?/trans>textBox1.Text包含:

    • (A)非數(shù)字(除外)

      space

      在開始/結(jié)束時(shí),

      -

      (一開始)和/或
    • (B)您的代碼的應(yīng)用區(qū)域性中有1000個(gè)分隔符,而沒有指定

      NumberStyles.AllowThousands

      或者您指定

      NumberStyles.AllowThousands

      但錯(cuò)了

      thousand separator

      在文化和/或
    • (C)十進(jìn)制分隔符(不應(yīng)存在于

      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!

似乎不確定,但實(shí)際上是好的例子:

案例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調(diào)試器,并確保它具有完全可以接受的數(shù)字格式。int范圍。就像這樣:

1234

此外,你也可以考慮

  1. 使用

    TryParse

    而不是

    Parse

    若要確保非解析數(shù)字不會(huì)導(dǎo)致異常問題,請執(zhí)行以下操作。
  2. 檢查…的結(jié)果TryParse如果不處理的話true

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


查看完整回答
反對 回復(fù) 2019-06-28
  • 3 回答
  • 0 關(guān)注
  • 1057 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)