Advertisement
blackpab

Zgadywanka

Oct 30th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package zgadywanka;
  7.  
  8. /**
  9. *
  10. * @author Emperor
  11. */
  12. public class Zgadywanka {
  13.  
  14. /**
  15. * @param args the command line arguments
  16. */
  17. public static void main(String[] args) {
  18. int ileElementow = 1000;
  19. System.out.println("Nastepuje zwolnienie blokady. Losujemy od 1-" + ileElementow);
  20. int wylosowana = (int) (Math.random() * ileElementow) + 1;
  21. System.out.println("System wylosowal: " + wylosowana);
  22.  
  23. int zgaduje = ileElementow / 2;
  24. int pierwszy = 1;
  25. int ostatni = ileElementow;
  26. int temp;
  27. int licznik = 1;
  28.  
  29. System.out.println("\tPIE:" + pierwszy + ", OST:" + ostatni);
  30. while (true) {
  31. System.out.print((licznik++) + " Zgaduje ze to: " + zgaduje);
  32. if (zgaduje == wylosowana) {
  33. break;
  34. } else if (zgaduje < wylosowana) {
  35. System.out.println(" < " + wylosowana);
  36. temp = zgaduje;
  37. zgaduje = (ostatni - zgaduje + 1) / 2 + zgaduje;
  38. pierwszy = temp;
  39.  
  40. } else {
  41. System.out.println(" > " + wylosowana);
  42. temp = zgaduje;
  43. zgaduje = (zgaduje - pierwszy + 1) / 2 + pierwszy;
  44. ostatni = temp;
  45. }
  46. System.out.println("\tPIE:" + pierwszy + ", OST:" + ostatni);
  47.  
  48. }
  49. System.out.println(" = " + wylosowana);
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement