Проверка ввода данных - C#
Формулировка задачи:
using System;
namespace Satan
{
class Program
{
static void Main(string[] args)
{
int c_ = 0;
do
{
int a, b, c;
Console.WriteLine("Во имя Сатаны, введите a, b, c:");
a = Convert.ToInt32(Console.ReadLine());
b = Convert.ToInt32(Console.ReadLine());
c = Convert.ToInt32(Console.ReadLine());
TheEquationOfSatan y = new TheEquationOfSatan(a, b, c);
Console.ReadKey();
do
{
try
{
Console.Write("Еще? (DA 1|Noooooo 0)");
c_ = Convert.ToInt32(Console.ReadLine());
break;
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
Console.WriteLine("Попробуйте еще раз.");
}
} while ((!(c_ == 0)) || (!(c_ == 1)));
} while (c_ == 1);
}
}
class TheEquationOfSatan
{
public int a
{
get;
set;
}
public int b
{
get;
set;
}
public int c
{
get;
set;
}
public double X1
{
get;
private set;
}
public double X2
{
get;
private set;
}
public TheEquationOfSatan(int a, int b, int c)
{
this.a = a;
this.b = b;
this.c = c;
if ((b * b - 4 * a * c) >= 0)
coren();
else
Console.WriteLine("AVE SATAN");
}
private void coren()
{
X1 = (-b + Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
X2 = (-b - Math.Sqrt(b * b - 4 * a * c)) / (2 * a);
Console.WriteLine("x1= " + this.X1.ToString() + " x2= " + this.X2.ToString());
}
}
}Решение задачи: «Проверка ввода данных»
textual
Листинг программы
int a, b, c;
bool a666, b666, c666;
Console.WriteLine("Во имя Сатаны, введите a, b, c:");
do
{
a666 = Int32.TryParse(Console.ReadLine(), out a);
b666 = Int32.TryParse(Console.ReadLine(), out b);
c666 = Int32.TryParse(Console.ReadLine(), out c);
if (!(a666 && b666 && c666))
Console.WriteLine("Гори в аду! (Ну или повтори ввод a, b, c:)");
}
while (!(a666 && b666 && c666));