Error message indicates that it fails for i = 5
, but your loop is conditioned on the value of m
, so something is wrong with your loop.
Printing the value of i
and m
for each iteration up to i = 5
will show you the error:
for (int i=0, m=2; i <= 5; i++) {
m = (int) Math.pow(m, i);
System.out.println(i + ": " + m);
}
OUTPUT (Ideone)
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
As you can see, m
is always 1
.
That is because first iteration calculates:
i=0,m=2: 2⁰ = 1
i=1,m=1: 1¹ = 1
i=2,m=1: 1² = 1
i=3,m=1: 1³ = 1
i=4,m=1: 1⁴ = 1
i=5,m=1: 1⁵ = 1
Debugging your code should have easily shown you this, faster than getting answer here.
1
solved Cannot solve java.lang.ArrayIndexOutOfBoundsException