gpt4 book ai didi

java - 这是一个无限循环吗?我究竟做错了什么? (Java 方法)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:13:43 25 4
gpt4 key购买 nike

我应该编写一个“bisquare”方法,返回一个数字范围内的 bisquare 的数量。我以为我已经弄明白了,但是当我运行这段代码时,没有任何显示,我的笔记本电脑开始像疯了一样嗡嗡作响。系统从不说它完成了。

这是我的代码。我究竟做错了什么? (如果我没有正确设置它,我也在寻找解决问题的方法。)

   // An integer that is the sum of the squares of two other integers is called bisquare
// 5 is bisquare because it is 1*1 + 2*2
// 4 is bisquare because it is 0*0 + 2*2 (we can use 0 as one of our numbers)
// 8 is bisquare because it is 2*2 + 2*2 (we can use the same two numbers)
// 3 is not bisquare, 6 is not bisquare
//
// Given two int parameters, low and high, return the number of bisquares that
// fall between low and high (inclusive)
//
// EXAMPLES:
// low = 1, high = 6
// return 4
// 1, 2, 4, and 5 are bisquare. 3 and 6 are not
//
// low = 7, high = 7
// return 0
// 7 is not bisquare. that is the entire range we are checking.

public static int bisquare(int low, int high)
{
int count = 0;
boolean isBisquare = false;
for (int checkNum = low; checkNum < high; checkNum++) {
while (!isBisquare) {
for (int i = 0; i < high; i++) {
for (int j = 0; j < high; j++) {
if ((i*i) + (j*j) == low) {
count++;
isBisquare = true;
}
}
}
}
}
return count;
}

最佳答案

是的,如果 (i*i) + (j*j) == low 中没有一个计算结果为真,则 while 将无限循环。

关于java - 这是一个无限循环吗?我究竟做错了什么? (Java 方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com