Help with method. Finding minimum value in Array

So I trying to find the minimum value in the scoreboard array. I tried this

Code Java:

public int minValue(){// Finds the minimum value value in the array scoreboard
int x=0;
for(int j=0;j<13;j++){
int z=scoreboard[j];
if(scoreboard[j] < z)
x=scoreboard[j];
}
return x;
}

But it always returns zero, any help?

Re: Help with method. Finding minimum value in Array

Code :

int z=scoreboard[j];
if(scoreboard[j] < z)

You also need to carefully examine your code.

Lets say that the first element in the array(j is 0) is 10. That first line of code assigns 10 to z. You then ask if 10 < 10 which is never going to be true. Therefore the value of x never changes.

BTW please use meaningful variable names.

Re: Help with method. Finding minimum value in Array

so something like this...

Code Java:

public int minValue(){// Finds the minimum value value in the array scoreboard
int smallest=scoreboard[0];
for(int j=0;j<13;j++){
if(scoreboard[j] < smallest)
smallest=scoreboard[j];
}
return smallest;
}

Re: Help with method. Finding minimum value in Array

What happened when you tried it? Make sure you test it with several arrays containing a variety of values.

BTW hard coding the value 13 is not a good idea. Use the length of the array instead.