fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int top = 100, bottom = 1;
  13. int answer = 95;
  14. int guess;
  15. int over = 0;
  16. while (over < 50)
  17. {
  18. over++;
  19. guess = (top + bottom) / 2;
  20. if (guess > answer)
  21. {
  22. top = guess - 1;
  23. System.out.println("The guess is " + guess);
  24. }
  25. if (guess < answer)
  26. {
  27. bottom = guess + 1;
  28. System.out.println("The guess is " + guess);
  29. }
  30. if (guess == answer)
  31. {
  32. over = 50;
  33. }
  34. }
  35. System.out.println("Done");
  36. }
  37. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
The guess is 50
The guess is 75
The guess is 88
The guess is 94
The guess is 97
Done