Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.schule.test;
- import java.util.Scanner;
- public class Aufgabe5 {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- System.out.println("Gleichung: ax²+bx + c = 0");
- System.out.println("Bitte geben sie die 'a' ein: ");
- int a = input.nextInt();
- System.out.println("Bitte geben sie die 'b' ein: ");
- int b = input.nextInt();
- System.out.println("Bitte geben sie die 'c' ein: ");
- int c = input.nextInt();
- float discr = getDiscr(a, b, c);
- System.out.println("Discr: " + discr);
- if (discr < 0) {
- System.err.println("Es gibt keine Losüng!");
- return;
- }
- else {
- // x1,2 = (-b +- sqr(d)) / 2a
- // -5 + 7 / 2*2 = 2 / 4 = 0.5
- double x1 = (-b + Math.sqrt(discr)) / 2 * a;
- double x2 = (-b - Math.sqrt(discr)) / 2 * a;
- System.out.println("x1= " + x1 + ", x2= " + x2);
- }
- input.close();
- }
- static float getDiscr(int a, int b, int c) {
- // b**2 - 4ac
- float discr = (b * b) - (4 * a * c);
- return discr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement