I have been getting "operator !=/== cannot be applied to boolean,int" error
class Trianglestest {
public static void main(String[] args) {
final int a = 3;
final int b = 3;
final int c = 3;
if (a == b == c) {
System.out.println("Equilateral - all sides equal");
}
else if (a == b != c) {
System.out.println("Isosceles – two sides equal");
}
else if (b == c != a) {
System.out.println("Isosceles – two sides equal");
}
else if (a == c != b) {
System.out.println("Isosceles – two sides equal");
}
else if (a + b %26lt; c) {
System.out.println("Not a triangle - sum of two sides is less than the third side");
}
else if (b + c %26lt; a) {
System.out.println("Not a triangle - sum of two sides is less than the third side");
}
else if (a + c %26lt; b) {
System.out.println("Not a triangle - sum of two sides is less than the third side");
}
else if (a != b != c) {
System.out.println("Scalene - no sides equal");
}
}
}
Help pls! java program made in JCreator. program tells me triangle is isosceles when it shld be equilateral.?
I think the error is because you can only compare 2 values at one time, so you have to separate them into 2 sub-conditions.
if ( (a == b) %26amp;%26amp; (b == c) {
System.out.println("Equilateral - all sides equal");
}
else if ( (a == b) %26amp;%26amp; (b != c) {
System.out.println("Isosceles – two sides equal");
}
else if ( (b == c) %26amp;%26amp; (c != a) {
System.out.println("Isosceles – two sides equal");
}
else if ( (a == c) %26amp;%26amp; (c != b) {
System.out.println("Isosceles – two sides equal");
}
else if ( (a + b) %26lt; c) {
System.out.println("Not a triangle - sum of two sides is less than the third side");
}
else if ( (b + c) %26lt; a) {
System.out.println("Not a triangle - sum of two sides is less than the third side");
}
else if ( (a + c) %26lt; b) {
System.out.println("Not a triangle - sum of two sides is less than the third side");
}
else if ( (a != b) %26amp;%26amp; (b != c) {
System.out.println("Scalene - no sides equal");
}
*I've added brackets to the rest as well, it's a good practice to do that so you won't get the conditions wrong.
secret garden
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment