Method in Java that returns the first odd found in a range

时间:2015-10-31 00:07:01

标签: java

I need to create a method that receives an int n and tests all integers between n*n+1 and (n+1)*(n+1)-1 and returns the first odd number it encounters. If no such number is found between these bounds the function will return 0. I'm trying to figure out how to do it for a while and I don't get it. I'm quite new to Java. Can someone help me? I thought in something like that but Eclipse says me that This method must return a result of type int and didn't understand why. public static int test(int n){ // receives argument n int a = n*n+1; // calculate lower bound int b = (n+1)*(n+1)-1; // calculate upper bound do { for (int i = 1; i <=a; i++){ if (a % i == 0){ return a; } else if (a % i != 0){ return 0; } } ++a; } while (a <= b); }

6 个答案:

答案 0 :(得分:1)

To check whether a number is odd, use modulo: n % 2 == 0 if the above condition is evaluates to true, the number is even. This will evaluate to true if the number is odd: n % 2 != 0 I hope this helps.

答案 1 :(得分:0)

我会这样做:

public static int firstOdd(int n) {
    int a = n*n+1;             // calculate lower bound
    int b = (n+1)*(n+1)-1;     // calculate upper bound

    // if the lower bound isn't "between the two bounds" so start with the next number
    a++;

    // increase a until the upper bound is reached
    while(a < b) {
        // a is odd
        if(a % 2 == 1) {
            // first odd found
            return a;
        } 
        // try next number ( a = a + 1 )
        a++;
    }

    // no first odd found
    return 0;
}

使用%运算符,您可以测试数字是否为奇数:

if(number % 2 == 1) {...}

答案 2 :(得分:0)

 public static int test(int n){

        int a = n*n+1;             // calculate lower bound
        int b = (n+1)*(n+1)-1;     // calculate upper bound
        int result = 0;

        while (a < b){
            if (a % 2 == 1){
                result = a;
                break;
            }
            a++;
        }
        return result;
    }

答案 3 :(得分:0)

我会做像

这样的事情
  public void CountDown(final TextView v)
  {
    final int FinishNumber = 30;

    final CountDownTimer CountDown = new CountDownTimer((FinishNumber + 4)  * 100, 100)
    {
        int NumberToShow = 1;

        public void onTick(long millisUntilFinished)
        {
            if (NumberToShow < FinishNumber)
                v.setText(String.valueOf(NumberToShow++));
        }

        public void onFinish()
        {
            if (NumberToShow < FinishNumber)
                v.setText(String.valueOf(NumberToShow));
        }
    }
        .start();
}

这消除了从等式循环的需要。下限是奇数或偶数。如果是偶数,则保证下一个数字是奇数。

Eclipse正在为您提供该错误,因为并非所有代码路径都返回(即第一次迭代时的b>)。

答案 4 :(得分:0)

我认为你过分复杂了代码!

因为您正在检查边界之间的数字,所以不需要for循环。只需使用while循环即可开始使用&#39; a&#39;并迭代到&#39;#39;如果出现奇数,则返回奇数。如果没有,那么在函数结束时,它返回零。希望下面的代码有帮助!

public static int test(int n){

    int a = n*n+1;             // calculate lower bound
    int b = (n+1)*(n+1)-1;     // calculate upper bound

    while(a <= b){
        if(a % 2 != 0){     //checks if the number is odd, and if so
            return a;      // returns the value
        }

        ++a;               //iterates through range of B to A
    }

    return 0;              // Returns zero if no value is found
}

答案 5 :(得分:0)

为什么要使用两个循环,只需使用一个循环。使用数字%2!=来确定数字是否为奇数。

public static int test(int n) {      // receives argument n
    int a = n * n + 1;             // calculate lower bound
    int b = (n + 1) * (n + 1) - 1;     // calculate upper bound

    for (int i = a; i <= b; ++i) {
        if (i % 2 != 0) return i;
    }

    return -1;
}

或者简单地说:(因为范围是连续的)

public static int test(int n) {      // receives argument n
    int a = n * n + 1;             // calculate lower bound
    int b = (n + 1) * (n + 1) - 1;     // calculate upper bound
    // if a is not odd then a + 1 must be odd
    return a % 2 != 0 ? a : a + 1;
}