PDA

View Full Version : weird java program output...



sherlockmeister
10-03-2007, 02:23 AM
heres the code, if some one can tell me why the output is "3" instead of "0" i'll give you a million dollars (jk).


public class Q
{
public static void main(String[] args)
{
int[] t = {-10, 10, 10, -10};
int n = 0;
for (int i = 1; i < t.length; i++)
{
n = t[n] < t[i] ? n : i;
}
System.out.println(n);
}
}

edit: oops sorry for spamming but i realized what i was doing wrong...stupid me

IronTeapot
10-03-2007, 02:47 AM
well for one thing it doesnt look like you ever increase 'i'.

my thought of why it outputs 3 for n though is its outputting the array number that it stops at. cause if i gets to 4(t.length), then its in t[3] and its making n=3.