I tried to create a program that calculates the two roots of a second-degree equation. When I execute my code, it asks for the values of a
, b
and c
correctly, but at the time of displaying the result, it always returns "NaN".
My code is this, I do not know how to solve it:
package com.Class1;
import java.lang.*;
import java.util.Scanner;
public class Class1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Insira o valor de a:");
int a = in.nextInt();
System.out.println("Insira o valor de b:");
int b = in.nextInt();
System.out.println("Insira o valor de c:");
int c = in.nextInt();
double pB = Math.pow(b, 2);
double delta = pB - 4 * a * c;
double x1 = -1 * pB + Math.sqrt(delta) / 2 * a;
double x2 = -1 * pB - Math.sqrt(delta) / 2 * a;
double r1 = Math.round(x1);
double r2 = Math.round(x2);
System.out.println("A raíz x1 vale: "+ x1);
System.out.println("A raíz x2 vale: "+ x2);
}
}