I'm supposed to write two programs to determine the type of triangle, given side lengths. One program can use logical operators. One can not. Here's my solution to the first program:
if (S1 == S2 AND S2 == S3)
System.out.println("equalateral");
else if (S1 == S2 OR S2 == S3 OR S1 == S3)
System.out.println("isoceles");
else
System.out.println("scalene");
I think that part is correct, but I'm totally drawing a blank as to how I would NOT use logical operators. I thought maybe something with boolean, but that just goes back to me using logical operators. The exact directions are,
"Write the code using nested if-else statements, where every if has a matching else block and all if conditions are simple conditions, with no logical operators in them."
Can someone point me in the right direction?
First off, there is no AND
or OR
in Java. Try &&
and ||
.
But alternative to the logical operators
, you can use the comparison operators
.
if(S1 == S2) {
if(S2 == S3) {
//equilateral
} else {
//isosceles
}
} else if(S2 == S3) {
//isosceles
} else {
//scalene
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments