Reputation: 29
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");
Upvotes: 1
Views: 820
Reputation: 480
You can do write the if statement as follows:
if((a1==b2)&&(a2==b1))
Upvotes: 0
Reputation: 103018
What does a1 b2 = a2 b1 mean?
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.
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)
You get the gist - between ==
and &&
and perhaps some parentheses, you can do this.
Upvotes: 2
Reputation: 483
If you want parallel lines all you need is if (a1 == a2)
, the b1
and b2
don't matter.
However, if you want to check if the numbers multiplied together are equal use:
if ((a1 * b1) == (a2 * b2)) System.out.print("the lines are parallel");
and the compiler will calculate the expressions inside the parenthesis first before checking for equality.
Upvotes: 1
Reputation: 157
If your line equation is y = ax + b
then lines are parallel if a1 == a2
so your 'if statement' should be if (a1 == a2)
.
Upvotes: 2