I
I
iddmitriykozyrev2018-08-07 10:36:50
C++ / C#
iddmitriykozyrev, 2018-08-07 10:36:50

Codereview c# console app?

Check out a small C# console application for finding the discriminant and roots.
Help, please, with exception handling for variables a, b, c through try-catch.
Criticism is welcome, but not harsh. Still programming the second day after school pascal

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

namespace uravnenie1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Введите квадратное уравнение:");
                Console.WriteLine("Введите a,b,c:");
                
                double a = Convert.ToDouble(Console.ReadLine());
                double b = Convert.ToDouble(Console.ReadLine());
                double c = Convert.ToDouble(Console.ReadLine());

                double D = (double)(Math.Pow(b, 2)) - 4 * a * c;
                Console.WriteLine("квадратное выражение : " + a + "x^2+" + b + "x+" + c);
                Console.WriteLine("D=" + D);

                double x1;
                if (D > 0)
                {
                    double x2;
                    x1 = ((-b + (Math.Sqrt(D))) / (2 * a));
                    x2 = ((-b - (Math.Sqrt(D))) / (2 * a));
                    Console.WriteLine("X1=" + x1);
                    Console.WriteLine("X2=" + x2);
                }
                else if (D == 0)
                {
                    x1 = -b / (2 * a);
                    Console.WriteLine("X=" + x1);
                }
                else if (D < 0)
                {
                    Console.WriteLine("Действительных корней нет");
                }
                string exit = Console.ReadLine();
                if (exit == "exit")
                    break;
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LiptonOlolo, 2018-08-07
@LiptonOlolo

Errors that catch the eye (some of them are far-fetched(!):
1. Immediately convert data from a string to numbers:
It will break your program completely if you write a character to the console, or write nothing at all.
Use double.TryParse
2. Use string interpolation:
3. Extra brackets: What for? 4. Extra variables for what? We have calculated and output the result to the console, IF further the result is not needed for calculations:

Console.WriteLine($"X1 = {(-b + (Math.Sqrt(D))) / (2 * a)}");
Console.WriteLine($"X2 = {(-b - (Math.Sqrt(D))) / (2 * a)}");

5. To exit, it is not necessary to declare a variable:
if (Console.ReadLine() == "exit")
  break;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question