简体   繁体   中英

Bad operand types for binary operator - Pythagoreum triples

I am trying to figure out why this code is not working. I am trying to get the Pythagoreum triples, 1-500 , where the output values are distinct.

When I try to compile, I get an error message saying

error: bad operand types for binary operator.

What am I doing wrong?

public class Pythagoras {

    public static void main(String[] args) {
        int side1;
        int side2;
        int hypotenuse;
        for(side1 = 1; side1 <= 500; side1 ++)
            for(side2 = 1; side2 <= 500; side2 ++)
                for(hypotenuse = 1; hypotenuse <= 500; hypotenuse ++)
                    if(side1 < side2 < hypotenuse)
                        if((side1 * side1) + (side2 * side2) == (hypotenuse * hypotenuse))
                            System.out.printf(%d %d %d\n, side1, side2, hypotenuse);

    }

}

There are a couple of issues which need to be fixed:

  1. Blank spaces should never separate unary operators such as increment ("++"), and decrement ("--") from their operands.
  2. a < b < c is an invalid statement, and you need to use (a < b && b < c)

You can also get rid of the comparison ( side1 < side2 < hypotenuse ) by simply updating your for loops as shown below.

Updated implementation:

public class Pythagoras {

    public static void main(String[] args) {
        int side1;
        int side2;
        int hypotenuse;
        for(side1 = 1; side1 <= 500; side1++)
            for(side2 = side1+1; side2 <= 500; side2++)
                for(hypotenuse = side2+1; hypotenuse <= 500; hypotenuse++)
                    if((side1 * side1) + (side2 * side2) == (hypotenuse * hypotenuse))
                        System.out.printf("%d %d %d\n", side1, side2, hypotenuse);

    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM