Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. package com.schule.test;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Aufgabe5 {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner input = new Scanner(System.in);
  9.         System.out.println("Gleichung: ax²+bx + c = 0");
  10.         System.out.println("Bitte geben sie die 'a' ein: ");
  11.         int a = input.nextInt();
  12.         System.out.println("Bitte geben sie die 'b' ein: ");
  13.         int b = input.nextInt();
  14.         System.out.println("Bitte geben sie die 'c' ein: ");
  15.         int c = input.nextInt();
  16.         float discr = getDiscr(a, b, c);
  17.         System.out.println("Discr: " + discr);
  18.         if (discr < 0) {
  19.             System.err.println("Es gibt keine Losüng!");
  20.             return;
  21.         }
  22.         else {
  23.             // x1,2 = (-b +- sqr(d)) / 2a
  24.             // -5 + 7 / 2*2 = 2 / 4 = 0.5
  25.             double x1 = (-b + Math.sqrt(discr)) / 2 * a;
  26.             double x2 = (-b - Math.sqrt(discr)) / 2 * a;
  27.             System.out.println("x1= " + x1 + ", x2= " + x2);
  28.         }
  29.         input.close();
  30.     }
  31.  
  32.     static float getDiscr(int a, int b, int c) {
  33.         // b**2 - 4ac
  34.         float discr = (b * b) - (4 * a * c);
  35.         return discr;
  36.     }
  37.  
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement