Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未在for循环中初始化变量_Java - Fatal编程技术网

Java 未在for循环中初始化变量

Java 未在for循环中初始化变量,java,Java,所以,当我遇到这个问题时,我一直在CodingBat网站上练习我的Java编程技能。在其中,您必须创建一个简单的方法,该方法接受一个动态长度的整数数组,检查数组中的元素是否按递增顺序(1、2、3、15678等),如果为真,则返回“true”,如果有一个整数顺序错误,则返回“false” 首先,我初始化一个名为“result”的布尔变量。然后,我遍历该方法传递的整数数组。如果当前索引值小于下一个索引值,我将“result”设置为“true”,并重复循环。否则,我会将“result”设置为“fals

所以,当我遇到这个问题时,我一直在CodingBat网站上练习我的Java编程技能。在其中,您必须创建一个简单的方法,该方法接受一个动态长度的整数数组,检查数组中的元素是否按递增顺序(1、2、3、15678等),如果为真,则返回“true”,如果有一个整数顺序错误,则返回“false”

首先,我初始化一个名为“result”的布尔变量。然后,我遍历该方法传递的整数数组。如果当前索引值小于下一个索引值,我将“result”设置为“true”,并重复循环。否则,我会将“result”设置为“false”,打破循环并将“result”设置为“false”。在FOR循环之后,我返回“result”

但是,我一直收到一条错误消息,“result”没有正确初始化。我可以理解JVM带来的混乱,但是我认为在IF/ELSE语句中设置“result”的值可以解决这个问题

以下是我迄今为止所做的代码副本:

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;
}
公共布尔分数递增(int[]分数){
布尔结果;
for(int i=0;i
如果分数有0个元素怎么办?;]

如果分数有0个元素怎么办?;]

在代码中,您将在for循环中返回结果(在if-else语句之后)


在return语句之前添加另一个括号(用于关闭for循环)

在代码中,您将在for循环中返回结果(在if-else语句之后)


在return语句之前添加另一个括号(以关闭for循环)

您只是在
返回结果之前缺少一个右括号(
}

建议简化代码(并处理0个元素的数组!),您可以将
result
初始化为
true
。然后,当您在数组上循环时,将
result
更改为
false
当且仅当您发现一个元素的顺序不正确时


还有一个警告。在
for
循环中使用元素
i+1
。想想当你得到数组中的最后一个元素(
i==scores.length-1
)时会发生什么。

你只是在
返回结果之前缺少了一个右大括号(
}

建议简化代码(并处理0个元素的数组!),您可以将
result
初始化为
true
。然后,当您在数组上循环时,将
result
更改为
false
当且仅当您发现一个元素的顺序不正确时

还有一个警告。在
for
循环中使用元素
i+1
。想想当你得到数组中的最后一个元素(
i==scores.length-1
)时会发生什么情况。

如果(i<(i+1))
将始终计算为
true
。您需要在这些索引处比较数组的内容,而不是索引本身

比如:

public boolean scoresIncreasing(int[] scores) {
  for(int i = 0; i < scores.length-1; i++) {
    if(scores[i] > scores[i+1]) return false;
  }
  return true;
}
公共布尔分数递增(int[]分数){
对于(int i=0;i分数[i+1])返回false;
}
返回true;
}
如果(i<(i+1))
将始终计算为
true
。您需要在这些索引处比较数组的内容,而不是索引本身

比如:

public boolean scoresIncreasing(int[] scores) {
  for(int i = 0; i < scores.length-1; i++) {
    if(scores[i] > scores[i+1]) return false;
  }
  return true;
}
公共布尔分数递增(int[]分数){
对于(int i=0;i分数[i+1])返回false;
}
返回true;
}

首先,i 您需要的是分数[i] 因此,您的代码已修复:

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;
}
公共布尔分数递增(int[]分数){
布尔结果;
for(int i=0;i
试试这个作为替代。它的工作原理是,一旦你得到一个错误,你可以立即退出

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;
}
公共布尔分数递增(int[]分数){
布尔结果=true;//假定为true
for(int i=0;i分数[(i+1)]返回false;
}//缺少大括号
返回结果;
}

当然,您可能希望在开始时引入边界检查,以确保至少有两个值。

首先,i 您需要的是分数[i] 因此,您的代码已修复:

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;
}
公共布尔分数递增(int[]分数){
布尔结果;
for(int i=0;i
试试这个作为替代。它的工作原理是,一旦你得到一个错误,你可以立即退出

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;
}
公共布尔分数递增(int[]分数){
布尔结果=true;//假定为true
for(int i=0;i分数[(i+1)]返回false;
}//缺少大括号
返回结果;
}
当然,您可能需要引入边界c