简体   繁体   English

二进制运算符的错误操作数类型-Pythagoreum三元组

[英]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. 我正在尝试获取Pythagoreum三元组1-500 ,其中输出值是不同的。

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) a < b < c是无效的语句,您需要使用(a < b && b < c)

You can also get rid of the comparison ( side1 < side2 < hypotenuse ) by simply updating your for loops as shown below. 您还可以通过简单地更新如下所示的for循环来摆脱比较( side1 < side2 < hypotenuse )。

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);

    }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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