Advertisement
winone1208

Java zad14

Mar 23rd, 2021
934
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 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=0;
  17.         int liczba = 2;
  18.  
  19.         if (a != 0) {
  20.             delta = Math.pow(b, 2) - 4 * a * c;
  21.             liczba = (int) Math.signum(delta);
  22.         }
  23.  
  24.  
  25.         switch(liczba){
  26.             case -1:
  27.                 System.out.println("Brak rozwiązania");
  28.                 break;
  29.             case 0:
  30.                 x1 = -b / (2 * a);
  31.                 System.out.println("x: " + wyswietlDwaMiejsca(x1));
  32.                 break;
  33.             case 1:
  34.                 x1 = (-b - Math.sqrt(delta)) / (2 * a);
  35.                 x2 = (-b + Math.sqrt(delta)) / (2 * a);
  36.  
  37.                 System.out.println("x1: " + wyswietlDwaMiejsca(x1) + ", x2: " + wyswietlDwaMiejsca(x2));
  38.                 break;
  39.             default:
  40.  
  41.                 if (b != 0) {
  42.                     x1 = (-c / b);
  43.                     System.out.println("x: " + wyswietlDwaMiejsca(x1));
  44.                 } else {
  45.  
  46.                     if (c != 0) {
  47.                         System.out.println("Sprzeczność");
  48.                     } else {
  49.                         System.out.println("Zbiór R");
  50.                     }
  51.  
  52.                 }
  53.         }
  54.  
  55.  
  56.     }
  57.  
  58.  
  59.     static double wyswietlDwaMiejsca(double number1) {
  60.  
  61.  
  62.         // Zaokrąglenie do 2 miejsca po przecinku:
  63.         number1 *= 100;
  64.         number1 = Math.round(number1);
  65.         number1 /= 100;
  66.  
  67.         return number1;
  68.     }
  69.  
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement