Answer the question
In order to leave comments, you need to log in
Why does the array give the wrong result?
public class Main {
static void bar() {
int[] nums = {1, 2, 3};
for ( int x = 0; x < nums.length; x++ ) {
x += nums[x];
System.out.println(x);
}
}
public static void main(String[] args) {
Main.bar();
}}
1
5
Answer the question
In order to leave comments, you need to log in
First, the array does not produce anything anywhere.
Secondly, you use the same variable to iterate over the array and for the accumulator. Use different ones.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question