Skip to content

Commit

Permalink
Ввод исходных данных с консоли или через аргументы командной строки
Browse files Browse the repository at this point in the history
  • Loading branch information
stden committed May 23, 2015
1 parent bb06ad4 commit 469978a
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/java/QuadraticEquation.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Scanner;

/**
* Квадратное уравнение
*/
Expand Down Expand Up @@ -30,4 +32,36 @@ public static double[] solve(double a, double b, double c) {
double d = Math.sqrt(D);
return new double[]{(-b - d) / (2 * a), (-b + d) / (2 * a)};
}

/**
* Ввод коэффициентов с консоли или из командной строки
*
* @param args Аргументы командной строки, например: java QuadraticEquation 1.0 -2.0 1.0
*/
public static void main(String[] args) {
double a, b, c;
if (args.length == 3) {
a = Double.parseDouble(args[0]);
b = Double.parseDouble(args[1]);
c = Double.parseDouble(args[2]);
} else {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите a: ");
a = scanner.nextDouble();
System.out.print("Введите b: ");
b = scanner.nextDouble();
System.out.print("Введите c: ");
c = scanner.nextDouble();
}
System.out.printf("%s x^2 + %s x + %s = 0%n", a, b, c);
try {
double roots[] = solve(a, b, c);
System.out.println("Количество решений = " + roots.length);
for (double x : roots) {
System.out.println("x = " + x + " -> " + (a * x * x + b * x + c));
}
} catch (AnyXException ex) {
System.out.println("Вырожденное уравнение: x - любое");
}
}
}

0 comments on commit 469978a

Please sign in to comment.