简体   繁体   English

变量未在for循环中初始化

[英]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. 所以当我遇到这个问题时,我一直在CodingBat网站上练习我的Java编程技巧。 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. 在它中,你必须创建一个简单的方法,它接受一个动态长度的整数数组,检查数组中的元素是否按递增顺序(1,2,3,15678等),并返回“true” “如果为真,或者如果有无序的整数则为”假“。

Firstly, I initialize a boolean variable named "result". 首先,我初始化一个名为“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. 如果当前索引值小于下一个索引值,我将“result”设置为“true”,并重复循环。 Else, I'll set "result" to "false", break out of the loop and set "result" to "false". 否则,我将“结果”设置为“假”,打破循环并将“结果”设置为“假”。 After the FOR loop, I return "result". 在FOR循环之后,我返回“结果”。

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. 我可以理解与JVM的混淆,但是我认为在IF / ELSE语句中设置“result”的值可以解决这个问题。

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. 首先,i <i + 1将始终为真,除非i = Integer.maxValue,在这种情况下,您将回绕到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. 你想要的是得分[i] <得分[i + 1],你需要调整你的循环值,以避免最后一次迭代的索引越界。

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 . return result之前,你只是缺少一个右括号( } )。

As a suggestion to simplify the code (and deal with arrays of 0 elements!), you may want to initialize result to true . 作为简化代码(并处理0个元素的数组!)的建议,您可能希望将result初始化为true Then, as you loop over the array, change result to false if and only if you find an element out of order. 然后,当您遍历数组时,将result更改为false当且仅当您发现元素乱序时。

One other word of caution. 另一个警告的话。 You use element i + 1 in your for loop. 您在for循环中使用元素i + 1 Think about what will happen when you get the last element in the array ( i == scores.length - 1 ). 想想当你得到数组中的最后一个元素时会发生什么( i == scores.length - 1 )。

if (i < (i + 1)) will always evaluate to true . if (i < (i + 1))将始终评估为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? 如果分数有0个元素怎么办? ;] ]

in your code, you are returning result inside the for loop (after the if-else statement). 在您的代码中,您将在for循环内返回结果(在if-else语句之后)。

Add another bracket before the return statement (to close the for loop) 在return语句之前添加另一个括号(关闭for循环)

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

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