for循环中的变量显示消息:“未使用局部变量i的值”。

huangapple go评论59阅读模式
英文:

Variable in for loop is giving a message that "The value of the local variable i is not used"

问题

// 我写了一个for循环,它的目的是确定是否有用户输入。如果有输入,它将`int[] valueArr`的6个元素设置为输入,一个vararg `int[] statValue`。如果没有输入,它将所有元素都设置为-1。

if (statValue.length == 6) {
    for (int i = 0; i < 6; i++) {
        valueArr[i] = statValue[i];
    }
} else {
    for (int i : valueArr) {
        i = -1;
    }
}

// 我正在使用Visual Studio Code,并且它在`for (int i : valueArr)`中给我一个消息:

// > "The value of the local variable i is not used."

// 这种for循环语法对我来说还是比较新的,所以我可能很盲目,但在另一个文件中它是有效的:

// for(int i : rollResults) {
//     sum = sum + i;
// }

// 我觉得我也应该提到,给我麻烦的for循环位于一个`private void`方法中。我还是相当新手,最近才开始使用私有方法。我注意到当它在其他地方没有被使用时,该方法会显示相同的消息,但我不明白为什么会在这里出现。

// 我尝试关闭和重新打开Visual Studio Code,删除并重新输入代码,以及其他形式的操作。在我的短暂经验中,我曾遇到过不应该出现的错误和消息,通过我提到的方法进行修复,但在这里没有起作用。
英文:

I wrote a for loop that is supposed to determine if there is user input. If there is, it sets the 6 elements of int[] valueArr to the input, a vararg int[] statValue. If there is no input, it sets all elements equal to -1.

if (statValue.length == 6) {
    for (int i = 0; i < 6; i++) {
        valueArr[i] = statValue[i];
    }

} else {
    for (int i : valueArr) {
        i = -1;
    }
}

I am using Visual Studio Code, and it is giving me a message in for (int i : valueArr) :

> "The value of the local variable i is not used."

That particular for loop syntax is still new to me, so I may be very well blind, but it was working in another file:

for(int i : rollResults) {
    sum = sum + i;
}

I feel that I should also mention that the for loop giving me trouble is in a private void method. I'm still fairly new and just recently started using private methods. I noticed the method would give the same message when not used elsewhere, but I do not see why it would appear here.

I tried closing and reopening Visual Studio Code, deleting and retyping the code, and other forms of that. In my short experience, I've had times where I received errors and messages that should not be there and fixed them with what I mentioned, but none of that worked here.

答案1

得分: 2

for (int i : valueArr) {
  .... CODE HERE ...
}

这段代码设置了一个循环,它将在一定次数内运行 CODE HERE。在这个循环的每一次迭代开始时,会创建一个全新的变量,命名为 i,其中包含了 valueArr 中的一个值。一旦循环结束,这个变量就会被销毁。需要注意的是,i 并不直接等于 valueArr 中的值 - 修改它__没有任何作用__ - 除非你稍后在块内部使用 i,这会影响这个循环。它__不会__修改 valueArr 的内容。

因此,你会收到警告:i = -1 什么都不做 - 你改变了 i 的值,然后循环结束,这意味着 i 会消失,你的代码没有做任何事情或产生任何效果,这肯定不是你的意图。因此,会收到警告。

这里不是完全清楚你想要做什么。如果你打算将 valueArr 中的所有值都设置为 -1,你可以使用以下代码:

for (int i = 0; i < valueArr.length; i++) valueArr[i] = -1;

或者,实际上,你可以更简单地这样做:

Arrays.fill(valueArr, -1);

valueArr[i] = -1 会将 valueArr 数组中第 i 个值更改为 -1。而 for (int i : valueArr) i = -1;什么都不做

英文:

> java
&gt; for (int i : valueArr) {
&gt; .... CODE HERE ...
&gt; }
&gt;

This sets up a loop which will run CODE HERE a certain number of times. Inside this loop, at the start of every loop, an entirely new variable is created named i, containing one of the values in valueArr. Once the loop ends this variable is destroyed. Notably, i is not directly the value in valueArr - modifying it does nothing - other than affect this one loop if you use i later in within the block. It does not modify the contents of valueArr.

Hence why you get the warning: i = -1 does nothing - you change what i is, and then the loop ends, which means i goes away and your code hasn't changed anything or done anything, which surely you didn't intend. Hence, warning.

It's not entirely clear what you want to do here. If you intend to set all values in valueArr to -1, you want:

for (int i = 0; i &lt; valueArr.length; i++) valueArr[i] = -1;

Or, actually, you can do that more simply:

Arrays.fill(valueArr, -1);

valueArr[i] = -1 changes the value of the i-th value in the valueArr array to -1. for (int i : valueArr) i = -1; does nothing.

huangapple
  • 本文由 发表于 2023年2月18日 08:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75490388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定