单击按钮后程序无响应

时间:2018-11-26 05:11:14

标签: c# winforms

我知道它并不漂亮,但是我在编程方面还是很新。 在我输入n1,n2和n3并按计算后,表格得到一个 “程序无响应”

在调试和发布模式下都这样做,并且我没有可见的错误消息。谁能帮助我?这是为明天到期的作业,现在我看不到修复程序,因此非常感谢您的帮助。

如果您需要检查代码的副本,请给我发一个下午。

亲切的问候。 Mathias

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace I3_REVISI
{
    public partial class Newton : Form
{
    public static double n1, n2, n3, x_NEWTON, Initialization, x1, approx1, approx2, approx3, approx4, approx5, approx6;
    public Newton()
    {
        InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.Show();
        this.Hide();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox8_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox7_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox11_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        textBox6.Text = "";
        textBox10.Text = "";
        textBox13.Text = "";
        textBox3.Text = "";
        Newton1.Text = "";
    }

    private void button3_Click(object sender, EventArgs e)
    {
        n1 = Convert.ToDouble(textBox6.Text);
        n2 = Convert.ToDouble(textBox10.Text);
        n3 = Convert.ToDouble(textBox13.Text);
        // Initialization If value is positive the start value is xinitialization -1 f(0) = xinitialization-1

        for (x_NEWTON = 0; x1 >= 0; x_NEWTON++)
        {
            x1 = n1 * Math.Pow(x_NEWTON, 2) + n2 * x_NEWTON + n3;
            Initialization = x1 - 1;
        }

        // Formula:   x1 = x0 - ( f(x0) / f'(x0) )
            approx1 = Initialization - ((n1*Math.Pow(Initialization,2)+n2*Initialization+n3) / (n1*2*Initialization-n2));
        approx2 = approx1 - ((n1 * Math.Pow(approx1, 2) + n2 * approx1 + n3) / (n1 * 2 * approx1 - n2));
        approx3 = approx2 - ((n1 * Math.Pow(approx2, 2) + n2 * approx2 + n3) / (n1 * 2 * approx2 - n2));
        approx4 = approx3 - ((n1 * Math.Pow(approx3, 2) + n2 * approx3 + n3) / (n1 * 2 * approx3 - n2));
        approx5 = approx4 - ((n1 * Math.Pow(approx4, 2) + n2 * approx4 + n3) / (n1 * 2 * approx4 - n2));
        approx6 = approx5 - ((n1 * Math.Pow(approx5, 2) + n2 * approx5 + n3) / (n1 * 2 * approx5 - n2));

        Newton1.Text = ("" + approx6);

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox10_TextChanged(object sender, EventArgs e)
    {

    }
}
}

1 个答案:

答案 0 :(得分:0)

  1. 检查您的输入值数据类型,并防止输入字符串或值 导致您的公式像被零除一样崩溃
  2. 将您的for循环索引从值更改为更少的值

    for(int i = 0; i <10)

  3. ,并将代码保留在try catch块中并传递异常参数 看看你的错误。

    try
    {
        n1 = Convert.ToDouble(textBox6.Text);
        n2 = Convert.ToDouble(textBox10.Text);
        n3 = Convert.ToDouble(textBox13.Text);
        f(0) = xinitialization-1;
    
        for (x_NEWTON = 0; x1 >= 0; x_NEWTON++)
        {
            x1 = n1 * Math.Pow(x_NEWTON, 2) + n2 * x_NEWTON + n3;
            Initialization = x1 - 1;
        }
    
            approx1 = Initialization - ((n1*Math.Pow(Initialization,2)+n2*Initialization+n3) / (n1*2*Initialization-n2));
        approx2 = approx1 - ((n1 * Math.Pow(approx1, 2) + n2 * approx1 + n3) / (n1 * 2 * approx1 - n2));
        approx3 = approx2 - ((n1 * Math.Pow(approx2, 2) + n2 * approx2 + n3) / (n1 * 2 * approx2 - n2));
        approx4 = approx3 - ((n1 * Math.Pow(approx3, 2) + n2 * approx3 + n3) / (n1 * 2 * approx3 - n2));
        approx5 = approx4 - ((n1 * Math.Pow(approx4, 2) + n2 * approx4 + n3) / (n1 * 2 * approx4 - n2));
        approx6 = approx5 - ((n1 * Math.Pow(approx5, 2) + n2 * approx5 + n3) / (n1 * 2 * approx5 - n2));
    
        Newton1.Text = ("" + approx6);
    }
    catch (exception ex)
    {
        messagebox.show(ex.message);
    }