如何用c#求解三个变量的方程组

时间:2016-10-26 11:18:34

标签: c# math

我想尝试做到这一点,但我可以使用一些帮助,当我看到整个代码在我面前时,我通常学得更好,但我也想要一些解释。我是初学者,如果可以的话,你能否使用方法让我更好地理解它们,因为这就是我在学习的东西。这是我到目前为止所做的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Three_Dimensions
         {
            class Program
     {
        static void Main(string[] args)
        {
            Console.WriteLine("Input your x");
            var g = Console.ReadLine();
            int x = Convert.ToInt32(g);

            Console.WriteLine("Input your y");
            var f = Console.ReadLine();
            int y = Convert.ToInt32(f);

            Console.WriteLine("Input your z");
            var l = Console.ReadLine();
            int z = Convert.ToInt32(l);

            Console.WriteLine(x + " " + y + " " + z);
            Console.ReadLine();

            Console.WriteLine("Now we are on the second equation. Press Enter");
            Console.ReadLine();

            Console.WriteLine("Input your x");
            var p = Console.ReadLine();
            int a = Convert.ToInt32(p);                                    // variables a b c

            Console.WriteLine("Input your y");
            var h = Console.ReadLine();
            int b = Convert.ToInt32(h);

            Console.WriteLine("Input your z");
            var v = Console.ReadLine();
            int c = Convert.ToInt32(v);

            Console.WriteLine(x + " " + y + " " + z);
            Console.WriteLine(a + " " + b + " " + c);
            Console.ReadLine();

            Console.WriteLine("Now we are on the third equation. Press Enter");
            Console.ReadLine();


            Console.WriteLine("Input your x");
            var plol = Console.ReadLine();
            int ab = Convert.ToInt32(plol);

            Console.WriteLine("Input your y");                              //variables ab bb cb
            var lol = Console.ReadLine();
            int bb = Convert.ToInt32(lol);

            Console.WriteLine("Input your z");
            var olo = Console.ReadLine();
            int cb = Convert.ToInt32(olo);

            Console.WriteLine(x + " " + y + " " + z);
            Console.WriteLine(a + " " + b + " " + c);
            Console.WriteLine(ab + " " + bb + " " + cb);
            Console.ReadLine();

            Console.WriteLine("Thank you now the process begins");    
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码没有任何问题,但可以稍微进行优化,因此请考虑将其发布在Code Review上。你可能会在那里得到更多有用的答案。

您可以使用输入循环来简化“UI” - 代码,如下所示:

  List<int[]> inputs = new List<int[]>();
  string[] headers = { "First equation: ", "Second equation: ", "Third equation: " };
  string[] prompts = { "Enter x: ", "Enter y: ", "Enter z: " };

  for (int i = 0; i < 3; i++)
  {
    int[] input = new int[3];

    Console.WriteLine(headers[i]);
    for (int j = 0; j < 3; j++)
    {
      Console.Write(prompts[j]);
      input[j] = int.Parse(Console.ReadLine());
    }

    inputs.Add(input);
    Console.WriteLine();
  }

  foreach (var input in inputs)
  {
    Console.WriteLine(string.Join(" + ", input));
  }

有很多方法可以解决三个方程组的系统。回过头来看一个关于如何在C#中实现它的问题,当你选择了一个并尝试使用自己的代码时。