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.
To be a second-degree equation, it would not be just the A that must be non-zero?
– user28595
@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.
– Jéf Bueno
@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
– Jefferson Quesado
Yes. The code is certainly incomplete
– Jéf Bueno
"There’s only one real solution: X1 and x2" Wait, I thought I had one
– mutlei
But in the book exercise that I’m studying, he asks to make a complete high school equation.
– Wellington Mazoni
@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.
– Wellington Mazoni