Error in java second degree equation algorithm

Asked

Viewed 275 times

4

Good morning, could someone help me understand why my code is not doing the correct processing and why my conditional deviation is wrong?

public void cap4ex3e(){
    double a,b,c,delta,x1,x2;
    Scanner in = new Scanner(System.in);
    a = in.nextFloat();
    b = in.nextFloat();
    c = in.nextFloat();
    if ((a!=0)&&(b!=0)&&(c!=0)){
        delta = Math.sqrt(b)-4*a*c;
        if (delta<0) {
            System.out.println("Não há solução real");
        } else if (delta>0){
            x1=(-b+Math.pow(delta,1/2))/(2*a);
            x2=(-b-Math.pow(delta,1/2))/(2*a);
            System.out.println("Há duas soluções reais: "+x1+"e"+x2);
        } else {
            x1=(-b+Math.pow(delta,1/2))/(2*a);
            x2=(-b-Math.pow(delta,1/2))/(2*a);
            System.out.println("Há apenas uma solução real: "+x1+"e"+x2);
        }
         System.out.println("Não é uma equação do 2º grau");
    }
}

When I assign the positive values to the variables a, b and c it falls into the conditional deviation

if (delta<0) {
    System.out.println("Não há solução real"); 

and:

System.out.println("Não é uma equação do 2º grau");

What wasn’t supposed to happen.

I believe it’s the way out in the wrong place of the condition but I can’t see it.

1 answer

10


The delta calculation is wrong, it must be

delta - bhaskara

currently stands as

delta errado - bhaskara

Note that the last message

It’s not a second-degree equation

is being shown always, I imagine that some validation of when to show it or not.

And also that in the part where there is only one real solution, the solution is printed twice.

I made some adjustments to the code, to make it more readable. In general, the error is only in the formula that calculates the delta.

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        double a, b, c;
        try {
            a = in.nextDouble();
            b = in.nextDouble();
            c = in.nextDouble();
        } finally {
            in.close();
        }

        if(a == 0 || b == 0 || c == 0) {
            System.out.println("A, B e C precisam ser diferentes de zero");
            return;
        }

        double delta = (b * b) - (4 * a * c);
        double x1, x2;

        if (delta < 0) {
            System.out.println("Não há solução real");
        } else if (delta > 0){
            x1 = (-b + Math.pow(delta, 1/2)) / (2 * a);
            x2 = (-b - Math.pow(delta, 1/2)) / (2 * a);
            System.out.println("Há duas soluções reais: " + x1 + " e " + x2);
        } else {
            x1 = (-b + Math.pow(delta, 1/2)) / (2 * a);
            x2 = (-b - Math.pow(delta, 1/2)) / (2 * a);
            System.out.println("Há apenas uma solução real: " + x1 + " e " + x2);
        }
    }
}

See working on repl.it

  • 2

    To be a second-degree equation, it would not be just the A that must be non-zero?

  • @Articuno Yes. Only in the complete equation that everyone has to be non-zero. But this is a validation of the original code, I just changed the order.

  • @Certainly. I also noticed that in the part where there is only one real solution is being printed two numbers: the root and it again

  • Yes. The code is certainly incomplete

  • "There’s only one real solution: X1 and x2" Wait, I thought I had one

  • But in the book exercise that I’m studying, he asks to make a complete high school equation.

  • 1

    @LINQ, thank you so much for the explanation and show where was the error in processing, now I will fix the outputs that are also wrong, but my real doubt was why the program wasn’t doing the delta processing and why the last exit was always coming out saying it wasn’t a second-degree equation.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.