错误“输入字符串格式不正确”.高斯法

本文关键字:不正确 高斯法 格式 字符串 输入 错误 | 更新日期: 2023-09-27 18:35:39

我在代码和运行它时遇到了一些麻烦。

我的代码:

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //
    textBox1.Text=("0.42");
    textBox2.Text=("1.14");
    textBox3.Text=("-0.71");
    textBox4.Text=("-1.13");
    textBox5.Text=("-2.15");
    textBox6.Text=("0.81");
    textBox7.Text=("7.05");
    textBox8.Text=("5.11");
    textBox9.Text=("-0.02");
    textBox10.Text=("6.15");
    textBox11.Text=("-4.16");
    textBox12.Text=("-0.17");
    }
    void Button1Click(object sender, EventArgs e)
    {
        double z1,z2,z3,x1,x2,x3,c1,c2,c3,a1,a2,a3,n,s1,s2,s3;          
        z1=Convert.ToDouble(textBox1.Text);
        z2=Convert.ToDouble(textBox2.Text);
        z3=Convert.ToDouble(textBox3.Text);
        x1=Convert.ToDouble(textBox4.Text);
        x2=Convert.ToDouble(textBox5.Text);
        x3=Convert.ToDouble(textBox6.Text);
        c1=Convert.ToDouble(textBox7.Text);
        c2=Convert.ToDouble(textBox8.Text);
        c3=Convert.ToDouble(textBox9.Text);
        a1=Convert.ToDouble(textBox10.Text);
        a2=Convert.ToDouble(textBox11.Text);
        a3=Convert.ToDouble(textBox12.Text);            
        n=x1/z1;            
        textBox24.Text=Convert.ToString(z1=n*z1);
        textBox23.Text=Convert.ToString(z2=n*z2);
        textBox22.Text=Convert.ToString(z3=n*z3);
        textBox19.Text=Convert.ToString(x1=x1-z1);
        textBox20.Text=Convert.ToString(x2=x2-z2);
        textBox21.Text=Convert.ToString(x3=x3-z3);
        textBox13.Text=Convert.ToString(a1=a1*n);
        textBox14.Text=Convert.ToString(a2=a2-a1);          
        n=c1/z1;            
        textBox24.Text=Convert.ToString(z1=n*z1);
        textBox23.Text=Convert.ToString(z2=n*z2);
        textBox22.Text=Convert.ToString(z3=n*z3);
        textBox16.Text=Convert.ToString(c1=c1-z1);
        textBox17.Text=Convert.ToString(c2=c2-z2);
        textBox18.Text=Convert.ToString(c3=c3-z3);
        textBox13.Text=Convert.ToString(a1=a1*n);
        textBox15.Text=Convert.ToString(a3=a3-a1);          
        n=c2/x2;
        textBox32.Text=Convert.ToString(x2=x2*n);
        textBox33.Text=Convert.ToString(x3=x3*n);
        textBox29.Text=Convert.ToString(c2=c2-x2);
        textBox30.Text=Convert.ToString(c3=c3-x3);
        textBox26.Text=Convert.ToString(a2=a2*n);
        textBox27.Text=Convert.ToString(a3=a3-a2);
        textBox36.Text=textBox24.Text;
        textBox35.Text=textBox23.Text;
        textBox34.Text=textBox22.Text;
        textBox25.Text=textBox13.Text;
        n=(1/c3);
        s1=Math.Round(n*a3,4);
        label28.Text=("c="+s1);
        s2=Math.Round((1/x2)*(a2-(x3*s1)),4);
        label29.Text=("b="+s2);
        s3=Math.Round((1/z1)*(a1-(z2*s2)-(z3*s1)),4);
        label30.Text=("a="+s3);     
    }
}

这个程序是高斯方法练习。

当我运行程序时,我尝试按下按钮,但随后弹出错误:

System.FormatException:输入字符串的格式不正确。   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)   at System.Convert.ToDouble(String value)   在gausa_metode_2_variants。MainForm.Button1Click(Object sender, EventArgs e) in c:''Users''NIKS''Documents''SharpDevelop Projects''gausa metode 2 variants''gausa metode 2 variants''MainForm.cs:line 48   at System.Windows.Forms.Control.OnClick(EventArgs e)   at System.Windows.Forms.Button.OnClick(EventArgs e)   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)   at System.Windows.Forms.Control.WmMouseUp(Message&m, MouseButton button, Int32 clicks)   at System.Windows.Forms.Control.WndProc(Message&m)   at System.Windows.Forms.ButtonBase.WndProc(Message& m)   at System.Windows.Forms.Button.WndProc(Message&m)   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)   at System.Windows.Forms.Application.Run(Form mainForm)   在gausa_metode_2_variants。Program.Main(String[] args) in c:''Users''NIKS''Documents''SharpDevelop Projects''gausa metode 2 variants''gausa metode 2 variants''Program.cs:line 27

错误“输入字符串格式不正确”.高斯法

在第

48 行中将指定的string转换为double时遇到问题。这将是**=Convert.ToDouble(textBox*.Text);之一.

根据地区和区域设置,我很确定您需要为小数分隔符编写,(至少一些国家使用 , 而不是 . )。

它无法解析字符串以在行上加倍

    z1=Convert.ToDouble(textBox1.Text);
    z2=Convert.ToDouble(textBox2.Text);
    z3=Convert.ToDouble(textBox3.Text);
    x1=Convert.ToDouble(textBox4.Text);
    x2=Convert.ToDouble(textBox5.Text);
    x3=Convert.ToDouble(textBox6.Text);
    c1=Convert.ToDouble(textBox7.Text);
    c2=Convert.ToDouble(textBox8.Text);
    c3=Convert.ToDouble(textBox9.Text);
    a1=Convert.ToDouble(textBox10.Text);
    a2=Convert.ToDouble(textBox11.Text);
    a3=Convert.ToDouble(textBox12.Text); 

当我检查文本框值时似乎很好。没有错,也许您正在运行时更改文本框的值。最后,错误消息说:

System.FormatException: Input string was not in a correct format.
   at System.Number.ParseDouble(String value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Convert.ToDouble(String value) at line 48

因此,只需检查您尝试在第 48 行解析

的值