Play one game.
int i;
// Fill the array with random data (hay?)
for (i=0; i<MAX; i++) {
haystack[i] = (int)(r.nextFloat() * MAX);
}
// Precondition for binary search is that data be sorted!
Arrays.sort(haystack);
// Look for needle in haystack
i = Arrays.binarySearch(haystack, NEEDLE);
if (i >= 0) { // Found it, we win.
System.out.println("Value " + NEEDLE +
" occurs at haystack[" + i + "]");
return true;
} else { // Not found, we lose.
System.out.println("Value " + NEEDLE +
" does not occur in haystack; nearest value is " +
haystack[-(i+2)] + " (found at " + -(i+2) + ")");
return false;
}