SRL Archive - Java Help and Tutorials

weird java program output...


sherlockmeister

2007-10-02 22:23:39

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

2007-10-02 22:47:10

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.