winone1208

Java zad13

Mar 23rd, 2021 (edited)
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. package pl.patrykk;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         System.out.println("ax^2+bx+c=0");
  9.         Scanner scanner = new Scanner(System.in);
  10.         System.out.println("Podaj a:");
  11.         int a = scanner.nextInt();
  12.         System.out.println("Podaj b:");
  13.         int b = scanner.nextInt();
  14.         System.out.println("Podaj c:");
  15.         int c = scanner.nextInt();
  16.         double x1, x2, delta;
  17.  
  18.  
  19.         if (a != 0) {
  20.  
  21.             delta = Math.pow(b, 2) - 4 * a * c;
  22.             System.out.println(delta);
  23.             if (delta > 0) {
  24.  
  25.                 x1 = (-b - Math.sqrt(delta)) / (2 * a);
  26.                 x2 = (-b + Math.sqrt(delta)) / (2 * a);
  27.  
  28.                 System.out.println("x1: " + wyswietlDwaMiejsca(x1) + ", x2: " + wyswietlDwaMiejsca(x2));
  29.  
  30.             } else {
  31.  
  32.                 if (delta == 0) {
  33.                     x1 = -b / (2 * a);
  34.                     System.out.println("x: " + wyswietlDwaMiejsca(x1));
  35.                 } else {
  36.                     System.out.println("Brak rozwiązania");
  37.                 }
  38.  
  39.             }
  40.  
  41.         } else {
  42.  
  43.             if (b != 0) {
  44.                 x1 = (-c / b);
  45.                 System.out.println("x: " + wyswietlDwaMiejsca(x1));
  46.             } else {
  47.  
  48.                 if (c != 0) {
  49.                     System.out.println("Sprzeczność");
  50.                 } else {
  51.                     System.out.println("Zbiór R");
  52.                 }
  53.  
  54.             }
  55.  
  56.         }
  57.  
  58.  
  59.     }
  60.  
  61.  
  62.     static double wyswietlDwaMiejsca(double number1) {
  63.  
  64.  
  65.         // Zaokrąglenie do 2 miejsca po przecinku:
  66.         number1 *= 100;
  67.         number1 = Math.round(number1);
  68.         number1 /= 100;
  69.  
  70.         return number1;
  71.     }
  72.  
  73.  
  74. }
Add Comment
Please, Sign In to add comment