简体   繁体   English

for循环Java返回值

[英]for-loop Java return value

I am very new to Java, sorry if the question is too simple. 我是Java的新手,如果问题太简单了,抱歉。 I am trying to evaluate whether an array is part of the fibonacci sequence. 我正在尝试评估数组是否是斐波那契序列的一部分。 I do not know how to return "true" value when the whole "for" loop does not break. 当整个“ for”循环未中断时,我不知道如何返回“ true”值。 Any ideas? 有任何想法吗? Thank you in advance! 先感谢您! This is what I have for now: 这是我现在拥有的:

public boolean checkFibb(ArrayList<Integer> array1) {
    int i;
    int fibb;

    if (array1.size() < 3) {
        System.out.println("Your array is too short!");
    } else {
        for (i = 0; i <= array1.size() - 2; i++) {
            fibb = array1.get(i + 2) - (array1.get(i + 1) + array1.get(i));

            if (fibb != 0) {
                System.out.println("Elements are not part of the Fibonacci sequence.");
                break;
            } else {
                System.out.println("Elements are part of the Fibonacci sequence.");
            }
        }
    }

    return true;
}

You always return true from the method. 您总是从该方法返回true You should do something as follows: 您应该执行以下操作:

public boolean checkFibb(ArrayList<Integer> array1) {
   int i;
   int fibb;
   boolean isFibb = true;

   if (array1.size() < 3) {
       System.out.println("Your array is too short!");
       isFibb = false;
   } else {
       for (i = 0; i <= array1.size() - 2; i++) {
           fibb = array1.get(i + 2) - (array1.get(i + 1) + array1.get(i));

           if (fibb != 0) {
                System.out.println("Elements are not part of the Fibonacci sequence.");
                isFibb = false;
                break;
           } else {
                System.out.println("Elements are part of the Fibonacci sequence."); 
           }
       }
   }
   return isFibb;
}

What you're doing in your code is you're breaking the current iteration of the loop when you detect that the elements aren't part of a fibonacci sequence. 您在代码中所做的是,当检测到元素不是斐波那契序列的一部分时,您正在破坏循环的当前迭代。 break only stops the current iteration of the loop that you are in. What you want to do is return false from the function at this point. break仅会停止当前循环的迭代。您要执行的操作是在此时从函数return false When you detect that the array is indeed a fibonacci sequence you would want to return true at this point. 当您检测到数组确实是斐波那契数列时,您此时将希望return true

If you array is too short, it cannot be a fibonacci sequence thus you would return false at this point. 如果数组太短,则不能是斐波那契序列,因此此时您将return false

public boolean checkFibb(ArrayList<Integer> array1) {
int i;
int fibb;

if (array1.size() < 3) {
    System.out.println("Your array is too short!");
    return false;
} else {
    for (i = 0; i <= array1.size() - 2; i++) {
        fibb = array1.get(i + 2) - (array1.get(i + 1) + array1.get(i));

        if (fibb != 0) {
            System.out.println("Elements are not part of the Fibonacci sequence.");
            return false;
        } else {
            System.out.println("Elements are part of the Fibonacci sequence.");
            return true;
        }
    }
}

} }

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

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