TooLargeTooSmall.java 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. /**
  4. * Write a description of class TooLargeTooSmall here.
  5. *
  6. * @author (your name)
  7. * @version (a version number or a date)
  8. */
  9. public class TooLargeTooSmall
  10. {
  11. Random rand = new Random();
  12. private int secretNumber = rand.nextInt(100)+1;
  13. // instance variables - replace the example below with your own
  14. /**
  15. * Constructor for objects of class TooLargeTooSmall
  16. */
  17. public TooLargeTooSmall()
  18. {
  19. // initialise instance variables
  20. int guess = 0;
  21. int count = 0;
  22. Scanner scan = new Scanner(System.in);
  23. System.out.println("Guess a number between 0 to 100");
  24. for (int i=0; i<=100; i++)
  25. {
  26. guess = scan.nextInt();
  27. if (guess>secretNumber)
  28. {
  29. System.out.println("Too large");
  30. count++;
  31. }
  32. else if (guess < secretNumber)
  33. {
  34. System.out.println("Too small");
  35. count++;
  36. }
  37. else
  38. {System.out.println("You are correct! \nNumber of guesses: " + (count+1));
  39. break;
  40. }
  41. }
  42. }
  43. }