简体   繁体   English

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

[英]Is this an infinite loop? What am I doing wrong? (Java method)

I'm supposed to write a "bisquare" method that returns the number of bisquares in a range of numbers. 我应该写一个“ bisquare”方法,该方法返回一定范围内的双平方数。 I thought I'd figured it out, but when I run this code, nothing displays and my laptop begins to whir like crazy. 我以为我想通了,但是当我运行这段代码时,什么也没有显示,我的笔记本电脑开始发疯。 The system never says it finished. 系统从不说已完成。

Here is my code. 这是我的代码。 What am I doing wrong? 我究竟做错了什么? (I am also looking for a solution to the problem if I am not setting it up correctly.) (如果我没有正确设置,我也在寻找问题的解决方案。)

   // 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均不为true,则while将无限循环。

There are a few things wrong with your code. 您的代码有些错误。

The first thing is the loop 首先是循环

while (!isBisquare) {
    // some code
}

The code in this loop is executed in precisely the same way each time, so if no bisquare is found the first time the code in the loop executes, biSquare is not set to true and will not be set to true in further iterations, leading to an infinite loop. 每次以完全相同的方式执行此循环中的代码,因此,如果在第一次执行循环中的代码时未找到bisquare,则biSquare不会设置为true,在以后的迭代中也不会设置为true,从而导致无限循环

The second problem is this line: 第二个问题是这一行:

if ((i*i) + (j*j) == low) {

I think this should be 我认为这应该是

if ((i*i) + (j*j) == checkNum) {

otherwise you always check for the lowest number in the range whether it is a bisquare. 否则,您始终会检查该范围内的最小数字是否为双平方。

Combine these two errors and you get an infinite loop whenever the argument low is not a bisquare, regardless of the value of high. 结合这两个错误,只要参数low不是双平方,无论high的值如何,都将导致无限循环。

EDIT: Initially I didn't notice what you meant to do with the while loop. 编辑:最初我没有注意到您打算用while循环做什么。 After reading some of the discussion I do realize it was to prevent counting one number multiple times. 阅读了一些讨论后,我意识到这是防止多次计数一个数字。 I suggest using @Clashsoft's second solution. 我建议使用@Clashsoft的第二个解决方案。 This makes the code much more readable and reusable. 这使代码更具可读性和可重用性。

You are not using the checkNum variable correctly. 您没有正确使用checkNum变量。 It should be used in the inner two for loops. 应该在内部的两个for循环中使用它。 Also, the while loop is unnecessary and creates an infinite loop for numbers that are not bisquares. 同样, while循环也是不必要的,并且会为非双平方的数字创建一个无限循环。

public static int bisquare(int low, int high)
{      
    int count = 0;
    for (int checkNum = low; checkNum < high; checkNum++)
    {
        outerloop:
        for (int i = 0; i < checkNum; i++)
        {
            for (int j = 0; j < checkNum; j++)
            {
                if (i * i + j * j == checkNum)
                {
                    count++;
                    break outerloop;
                }
            }
        }
    }
    return count;
}

For reasons of clarity, you should probably also consider creating a isBisquare(int) method like this: 为了清楚起见,您可能还应该考虑创建一个isBisquare(int)方法,如下所示:

public static boolean isBisquare(int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (i * i + j * j == n)
            {
                return true;
            }
        }
    }
    return false;
}

The bisquare method (which should have a better name, say countBisquares ) now looks like this: 现在, bisquare方法(应使用更好的名称,例如countBisquares )看起来像这样:

public static int countBisquares(int low, int high)
{      
    int count = 0;
    for (int checkNum = low; checkNum < high; checkNum++)
    {
        if (isBisquare(checkNum))
        {
            count++;
        }
    }
    return count;
}

Your logic was not correct. 您的逻辑不正确。 Below is the correct one: 下面是正确的一个:

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

I've tried some of the above solutions and I believe I have found an error in all of the above solutions. 我已经尝试了上述一些解决方案,但我相信在所有上述解决方案中都发现了错误。 In the all of the for loops, checkNum should be less than or equal to high, and i or j should be less than or equal to checkNum. 所有的for循环中,checkNum应小于或等于 high,i或j应小于或等于 checkNum。

Making these changes will give the answer 7 when given a high of 10 and a low of 1. without these changes, the answer is 5 given the same inputs. 进行这些更改将在高10和低1的情况下给出答案7。没有这些更改,在相同输入的情况下答案为5。

It don't think it will ever count actual high and low values unless this change is made. 除非进行此更改,否则它不会计算实际的高值和低值。

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

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