方程组程序错误

时间:2015-10-12 16:42:55

标签: c#

我制作了一个方程式系统,我没有得到正确的答案。我使用4x + y = 8和5x + 2y = 13来测试问题。正确的答案应该是x = 1,y = 4,但我得到x = .75和y = 5.我不知道错误是什么,但我会感谢一些帮助修复它。感谢

        Console.WriteLine("Enter row1 col1");
        string strX1 = Console.ReadLine();
        double dblX1 = Convert.ToDouble(strX1);

        Console.WriteLine("Enter row1 col2");
        string strY1 = Console.ReadLine();
        double dblY1 = Convert.ToDouble(strY1);

        Console.WriteLine("Enter row1 col3");
        string strZ1 = Console.ReadLine();
        double dblZ1 = Convert.ToDouble(strZ1);

        Console.WriteLine("Enter row2 col1");
        string strX2 = Console.ReadLine();
        double dblX2 = Convert.ToDouble(strX1);

        Console.WriteLine("Enter row2 col2");
        string strY2 = Console.ReadLine();
        double dblY2 = Convert.ToDouble(strY2);

        Console.WriteLine("Enter row2 col3");
        string strZ2 = Console.ReadLine();
        double dblZ2 = Convert.ToDouble(strZ2);

        double dblX1New = dblX2 * dblX1;
        double dblY1New = dblX2 * dblY1;
        double dblZ1New = dblX2 * dblZ1;
        double dblX2New = (dblX1 * -1) * dblX2;
        double dblY2New = (dblX1 * -1) * dblY2;
        double dblZ2New = (dblX1 * -1) * dblZ2;

        double dblX = dblX1New + dblX2New;
        double dblY = dblY1New + dblY2New;
        double dblZ = dblZ1New + dblZ2New;

        double dblYFinal = dblZ / dblY;
        double dblXFinal = (dblZ1 - dblYFinal * dblY1) / dblX1;

        Console.WriteLine("X: " + dblXFinal + " Y: " + dblYFinal);
        Console.ReadLine();

1 个答案:

答案 0 :(得分:0)

问题在于:

Console.WriteLine("Enter row2 col1");
string strX2 = Console.ReadLine();
double dblX2 = Convert.ToDouble(strX1); <--- Error

您正在转换strX1而不是strX2。结果,它认为两个X值是相同的。