简体   繁体   中英

Variable not initialized in for loop

So I've been practicing my Java programming skills on the CodingBat website, when I came across this problem. In it, you have to make a simple method that takes in an array of integers of dynamic length, check to see if the elements in the array are in increasing order (1, 2, 3, 15678, etc), and return "true" if true, or "false" if there is an integer out of order.

Firstly, I initialize a boolean variable named "result". Then, I iterate through the array of integers passed by the method. If the current index value is less than the next index value, I set "result" to "true", and repeat the loop. Else, I'll set "result" to "false", break out of the loop and set "result" to "false". After the FOR loop, I return "result".

However, I've been receiving an error message that "result" has not been initialized properly. I can kinda understand the confusing with the JVM, however I thought that setting the value for "result" inside of the IF/ELSE statements would solve that.

Here is a copy of the code that I have done so far:

public boolean scoresIncreasing(int[] scores) {
    boolean result;
    for (int i = 0; i < scores.length; i++) {
        if (i < (i + 1)) {
            result = true;
        }
        else {
            result = false;
            break;
        }
    }
    return result;
}

First of all, i < i+1 will always be true, unless i = Integer.maxValue, in which case you'll wrap around to Integer.minValue.

What you want is scores[i] < scores[i+1] , and you'll need to adjust your loop values to avoid an index out of bounds on the last iteration.

So, your code fixed:

public boolean scoresIncreasing(int[] scores) {
    boolean result;
    for (int i = 0; i < scores.length-1; i++) // fix loop end
    {
        if (scores[i] < scores[(i + 1)]) {
            result = true;
        }
        else {
            result = false;
            break;
        }
    } // missing brace
    return result;
}

Try this as an alternative. It works on the principle that once you get a false, you can get out immediately.

public boolean scoresIncreasing(int[] scores) {
    boolean result = true; // assume true
    for (int i = 0; i < scores.length-1; i++) // fix loop end
    {
        if (scores[i] > scores[(i + 1)]) return false;
    } // missing brace
    return result;
}

Of course, you may want to introduce bounds checking at the beginning to ensure that there are at least two values.

You are simply missing a closing brace ( } ) before return result .

As a suggestion to simplify the code (and deal with arrays of 0 elements!), you may want to initialize result to true . Then, as you loop over the array, change result to false if and only if you find an element out of order.

One other word of caution. You use element i + 1 in your for loop. Think about what will happen when you get the last element in the array ( i == scores.length - 1 ).

if (i < (i + 1)) will always evaluate to true . You need to compare the contents of the array at those indexes, not the indexes themselves.

Something like:

public boolean scoresIncreasing(int[] scores) {
  for(int i = 0; i < scores.length-1; i++) {
    if(scores[i] > scores[i+1]) return false;
  }
  return true;
}

What if scores has 0 elements? ;]

in your code, you are returning result inside the for loop (after the if-else statement).

Add another bracket before the return statement (to close the for loop)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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