0

这是我的代码:

     //a java application prints out all values of pythagorean triples     that        range from 1 to 500. 
      //uses a trippled nested for loop
    public class pythagoreantriples{

        //main method executes java application

        public static void main(String args[]){

       //declare variables


    for(int side_1=3;  side_1 <=500;  side_1++){

       for(int side_2=4; side_2 <=500; side_2++){

            for(int hypo=5; hypo <=500; hypo++){



                Math.pow(hypo,2)=Math.pow(side_1,2)+Math.pow(side_2,2);

                System.out.printf("n/%d/n",     Math.pow(side_1,2),"                 ", Math.pow(side_2,2),"                     ", Math.pow(hypo,2));



    } //end first for loop

    }//end second for loop

    } //end third for loop

    }//end main

    }//end class

这是我尝试编译代码时生成的错误:

>C:\Users\Courtney\Desktop\chapter five exercises for      >java\pythagoreantriples.java:17: error: unexpected type
            hypo*hypo=side_1*side_1+side_2*side_2;
                ^
  >required: variable
>found:    value
>C:\Users\Courtney\Desktop\chapter five exercises for 

>java\pythagoreantriples.java:19: error: unexpected type




  >Math.pow(hypo,2)=Math.pow(side_1,2)+Math.pow(side_2,2);
                ^
  >required: variable
  >found:    value
  >2 errors

  >Tool completed with exit code 1

我究竟做错了什么?它应该打印出一个范围从 1 到 500 的毕达哥拉斯三元组表。forloop 应该打印的第一个毕达哥拉斯三元组当然是 3^2+4^2=5^2

4

1 回答 1

0

您应该将失败的语句更改为条件表达式,替换=with ==,并在 if 语句中使用它来决定是否打印当前的三元组:

if (Math.pow(hypo, 2) == Math.pow(side_1, 2) + Math.pow(side_2, 2))

您还需要处理打印输出。Math.pow 的结果是双精度数,而不是整数。无论如何,您可能应该打印边,而不是正方形。

于 2015-05-16T11:12:00.107 回答