Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.lang.Math;
- public class Exercice3 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- System.out.print("Résolution de ax²+bx+c : \n");
- System.out.print(secondDegreeEquation());
- }
- public static String secondDegreeEquation() {
- double[] abc = lireEntier();
- double a = abc[0];
- double b = abc[1];
- double c = abc[2];
- double x0 = 0;
- double x1 = 0;
- double x2 = 0;
- double delta = ((b * b) - 4 * a * c);
- if (delta < 0) {System.out.print("L'équation n'a pas de résolution pour x réel. \n");}
- else if (delta == 0) {x0 = ((-b) / (2 * a)); System.out.print("L'équation a une solution : " + x0 + "\n");}
- else if (delta > 0) {x1 = ((-b) - Math.sqrt(delta)) / (2 * a);
- x2 = ((-b) + Math.sqrt(delta)) / (2 * a);
- System.out.print(("L'équation a deux solution possible :\n x1 = " + x1 + " et x2 = " + x2 +"\n"));}
- return "Discriminant : " + delta;
- }
- public static double[] lireEntier() {
- double a = 0;
- double b = 0;
- double c = 0;
- boolean isReal;
- do {
- try {
- Scanner inputUser = new Scanner(System.in);
- System.out.print("Rentrer un nombre réél pour a : ");
- a = inputUser.nextDouble();
- System.out.print("Rentrer un nombre réél pour b : ");
- b = inputUser.nextDouble();
- System.out.print("Rentrer un nombre réél pour c : ");
- c = inputUser.nextDouble();
- isReal = true;
- } catch (Exception e)
- {
- System.out.println("La valeur saisie n'est pas un réel");
- isReal = false;
- }
- } while (isReal != true);
- return new double[] {a, b , c};
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement