我需要找到 A 和 B 的值小于 100 的所有毕达哥拉斯三元组 (a^2+b^2=c^2)。我的代码没有给我正确的输出。谁能告诉我我的代码出了什么问题并改写正确的代码?
public static void main(String[] args) {
int sum = 0;
for (int num1 = 1; num1 <= 100; num1++) {
for (int num2 = 1; num2 < 100; num2++) {
if (PerfectSquare(num1, num2, sum)) {
System.out.println(num1 + " " + num2);
}
}
}
}
public static boolean PerfectSquare(int number1, int number2, int sum) {
int square1;
int square2;
double root;
int sum1;
number1 = (int) ((99 - 1 + 1) * Math.random() + 1);
number2 = (int) ((99 - 1 + 1) * Math.random() + 1);
square1 = (int) Math.pow(number1, 2.0);
square2 = (int) Math.pow(number2, 2.0);
sum = square1 + square2;
root = Math.sqrt(sum);
sum1 = (int) Math.pow(root, 2.0);
if (!(sum == sum1)) {
return false;
}
return true;
}