samedi 16 mars 2019

Writing an if statement for solving linear equations

I'm trying to write a code that will solve two linear equations. I have four coefficients, a11, a12, a21, a22 and two solutions b1, b2

I'd like to print out the solution if the determinant isn't 0, otherwise if b2a11 - b1a21 = 0 && b1a22 - b2a12 = 0 and a11, a12, a21, a22 not equals zero where b = 0 i want to print out just Many soltuions

if (a11*a22-a12*a21 != 0) {
    System.out.println("Single solution:(" +
    (double)(b1*a22-b2*a12)/(a11*a22-a12*a21) +","+
    (double)(b2*a11-b1*a21)/(a11*a22-a12*a21)+")");
}
else if (b2*a11-b1*a21==0 && b1*a22-b2*a12==0){
    if ((a11==0 && a12==0 && b1!=0)||(a21==0 && a22==0 && b2!=)) {
        System.out.println("No solution"); } else {
    System.out.println("Many solutions"); }
}
else { System.out.println("No solution"); }

The editor tells me I can't start an if statement with ) as written in line 7. The problem is that I have an OR and that's why I need the bracets, otherwise it'll be read as b1!=0 OR a21 ==0 which is not my intent.

Aucun commentaire:

Enregistrer un commentaire