Search code examples
javaif-statementvariablesdouble

how to put multiple variables in if statement in java?


I'm super new to programming , I'm making a program to calculate If two lines are parallel or not, the lines are considered parallel if a1 b2 = a2 b1 but I don't know how to put multiple variables in one if statement I have searched but I didn't find the answer can you help me please?

if (a1 && b2 == a2 && b1)
    System.out.print("the lines are parallel");
else System.out.print("the lines are not parallel");

Solution

  • What does a1 b2 = a2 b1 mean?

    It means: If a1 multiplied by b2 equals a2 multiplied by b1

    Then program that: a1 * b2 == a2 * b1. The habit of assuming 'multiply' when you put entries right next to each other is a math thing. In java that does not work; If you want to multiply, * is what you want.

    It means: if a1 equals b2, and a2 equals b1

    Then program that: && is 'and', as in, given two true/false values, you can use the && operator to reduce them to a single true/false value (true if both are true, false otherwise). Just like 5 + 7 reduces two numbers to one number, true && false does the same. Thus, you can't use && as "and" in the sense of "if a and b are equal to c" (because 'a' is not a true/false value). Thus:

    (a1 == b2) && (b1 == a2)

    Etcetera

    You get the gist - between == and && and perhaps some parentheses, you can do this.