I am learning Java after my first year as a computer science major using Python exclusively. I am making a basic program that solves quadratic equations. I am having issues with a variable not initializing when an if-statement is present in the program. Here is the code that doesn't compile:
public class Test
{
public static void main(String[] args)
{
String input;
do
{
Scanner in = new Scanner(System.in);
System.out.println("A: ");
double a = in.nextDouble();
System.out.println("B: ");
double b = in.nextDouble();
System.out.println("C: ");
double c = in.nextDouble();
if ((a == 0) && (b == 0) && (c == 0))
{
System.out.println("No solutions to quadratic formual with these inputs!");
continue;
}
double d = b * b - 4*a*c;
double x1 = (-b - Math.sqrt(d)) / (2 * a);
double x2 = (-b + Math.sqrt(d)) / (2 * a);
System.out.println(x1);
System.out.println(x2);
System.out.println("Continue? ");
input = in.next();
}
while (input.equals("Y"));
}
}
If I remove the if-statement, it compiles correctly. If I give the String input a default value of "", it also works correct (with the if-statement included). Why is this? This seems basic but I can't figure it out for the life of me!