123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Guessing game: TooLargeTooSmall.
  3. *
  4. * @author (Chistian)
  5. * @version (version1 10-19-18)
  6. */
  7. import java.util.*;
  8. import java.util.Scanner;
  9. public class Main {
  10. public static void main(String[] args){
  11. Random rand = new Random();
  12. int n = rand.nextInt(10) + 1;
  13. Scanner in = new Scanner(System.in);
  14. System.out.println("Please guess a number between 1 and 10");
  15. int guess = in.nextInt();
  16. int lastGuess = 0;
  17. int numberOfGuesses=1;
  18. while (n != guess){
  19. if (n < guess) {
  20. System.out.println("too large, guess another number");
  21. if (guess != lastGuess){
  22. lastGuess = guess;
  23. numberOfGuesses++;}
  24. }
  25. else if(n > guess) {
  26. System.out.println("too small, guess another number");
  27. if (guess != lastGuess){
  28. lastGuess = guess;
  29. numberOfGuesses++;}
  30. }
  31. guess = in.nextInt();
  32. }
  33. System.out.println("correct! you took " +String.format("%d",numberOfGuesses)+ " guesses to get it right");
  34. }
  35. }