12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Created by iyasuwatts on 10/17/17.
  3. */
  4. import java.util.Random;
  5. import java.util.Scanner;
  6. public class Game {
  7. public static void main(String[] args) {
  8. //creates random secret number 1-10
  9. Random number = new Random();
  10. int secretNumber = (number.nextInt(10) +1);
  11. //tracks number of guesses
  12. int numberOfGuesses = 0;
  13. //track previous guess
  14. int previousGuess = 0;
  15. //creates scanner for user input
  16. Scanner input = new Scanner(System.in);
  17. //variable to hold user guess
  18. int userGuess;
  19. //variable to hold win state
  20. boolean win = false;
  21. while (win == false) {
  22. //System asks for user input and stores user guess
  23. System.out.println("Guess a number between 1 and 10");
  24. userGuess = input.nextInt();
  25. //compares userGuess to secret number
  26. if (userGuess == secretNumber) {
  27. win = true;
  28. }
  29. else if (userGuess < secretNumber) {
  30. System.out.println("Too Low!");
  31. //increment guess counter if guess does not match
  32. //previous guess
  33. if(userGuess != previousGuess) {
  34. numberOfGuesses++;
  35. }
  36. previousGuess = userGuess;
  37. }
  38. else if (userGuess > secretNumber) {
  39. System.out.println("Too High!");
  40. if(userGuess != previousGuess) {
  41. numberOfGuesses++;
  42. }
  43. previousGuess = userGuess;
  44. }
  45. }
  46. numberOfGuesses++;
  47. System.out.println("You Win!");
  48. System.out.println("It took you " + numberOfGuesses + " tries");
  49. }
  50. }