I know this is probably an easy question. I'm writing a program that solves a system of three equations, using Cramer's Rule. This is not an assignment for a class, I'm just doing it for fun.
I wrote it initially on my TI-83+ graphing calculator, and I am now adapting it and writing it in Java. I think this should be right, but when I compile it I am getting 72 errors (!) that all say "cannot find symbol" - one for each time I'm using one of my variables in the formula that actually solves the system. (starting with the "double M = ").
When I used the same variables earlier in the program (when it displays the equations so you can check if they were inputted correctly), it is fine. But in the formula, it isn't.
Is it doing this because I declared the variables inside a "while" loop and am trying to use them outside of it?
How can I fix this?
Code:
import java.util.Scanner;
public class SolveASystemOf3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean inputIsCorrect = false;
while (inputIsCorrect == false) {
System.out.println("Find X, Y, and Z. \nInput the first equation. (Ax + By + Cz = D)");
System.out.print("A = ");
double A = input.nextDouble();
System.out.print("B = ");
double B = input.nextDouble();
System.out.print("C = ");
double C = input.nextDouble();
System.out.print("D = ");
double D = input.nextDouble();
System.out.println(" ");
System.out.println("Input the second equation. (Ex + Fy + Gz = H)");
System.out.print("E = ");
double E = input.nextDouble();
System.out.print("F = ");
double F = input.nextDouble();
System.out.print("G = ");
double G = input.nextDouble();
System.out.print("H = ");
double H = input.nextDouble();
System.out.println(" ");
System.out.println("Input the third equation. (Ix + Jy + Kz = L)");
System.out.print("I = ");
double I = input.nextDouble();
System.out.print("J = ");
double J = input.nextDouble();
System.out.print("K = ");
double K = input.nextDouble();
System.out.print("L = ");
double L = input.nextDouble();
System.out.println(" ");
System.out.println("Check if the input is correct.");
System.out.println(A + "x + " + B + "y + " + C + "z = " + D);
System.out.println(E + "x + " + F + "y + " + G + "z = " + H);
System.out.println(I + "x + " + J + "y + " + K + "z = " + L);
boolean goodResponse = false;
while (goodResponse == false) {
System.out.println("Is this correct? Enter 0 for no and 1 for yes.");
int inputAnswer = input.nextInt();
if (inputAnswer == 0) {
System.out.println("Re-enter your equations.");
goodResponse = true;
}
else if (inputAnswer == 1) {
inputIsCorrect = true;
goodResponse = true;
System.out.println(" ");
}
else {
System.out.println("Invalid response.");
}
}
}
//here is where it starts giving me trouble
double M = ((A * F * K) + (B * G * I) + (C * E * J) - (I * F * C) - (J * G * A) - (K * E * B));
if (M == 0) {
System.out.println("There is no single solution for this system. \nIt may have no solutions or it may have infinite solutions.");
}
else {
double X = (((D * F * K) + (B * G * L) + (C * H * J) - (L * F * C) - (J * G * D) - (K * H * B)) / M);
double Y = (((A * H * K) + (D * G * I) + (C * E * L) - (I * H * C) - (L * G * A) - (K * E * D)) / M);
double Z = (((A * F * L) + (B * H * I) + (D * E * J) - (I * F * D) - (J * H * A) - (L * E * B)) / M);
System.out.println("X = " + X);
System.out.println("Y = " + Y);
System.out.println("Z = " + Z);
}
}
}