简体   繁体   中英

How to solve systems of equations in three variables using c#

I would like to try to make this but I could use some help, I generally learn better when I see the whole code in front of me but i would also like some explanation. I am a beginner and if you could, could you use methods so I understand them better because that is what I am learning. This is what I have so far.

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

There is nothing wrong with your code, but it could be optimized a little bit, so consider to post it on Code Review instead. You'll maybe get more useful answers there.

You can use an input loop to ease the "UI"-code like this:

  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));
  }

There are many ways to solve a system of three equations. Come back with a question on how to implement it in C# when you have chosen one and tried with code for your self.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM