5
I tried to create a program that calculates the two roots of a second-degree equation. When I run my code, it asks for the values of a, b and c correctly, but when showing the result, it always returns "Nan".
My code is this, I don’t know how to solve:
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);
    }
}
						
Basically it’s because you’re trying to calculate the negative number root. Depending on the values, delta will be less than zero, and the sqrt will give Nan
– Bacco