1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Created by iyasuwatts on 10/17/17.
  3. */
  4. import java.util.Scanner;
  5. import java.util.Random;
  6. public class Main {
  7. public static void main(String[] args) {
  8. // Create random number from range of 0 to 100.
  9. Random rand = new Random();
  10. int mysteryNum = rand.nextInt(101);
  11. // Log number of user inputs.
  12. int userAttempts = 1;
  13. int userGuess;
  14. int prevGuess = 101;
  15. // Continue looping infinitely until correct answer is passed. Count each attempt.
  16. do {
  17. for (; ; userAttempts++) {
  18. Scanner input = new Scanner(System.in);
  19. System.out.println("What positive integer between 0 and 100 am I thinking of?");
  20. userGuess = input.nextInt();
  21. System.out.println("You guessed: " + userGuess);
  22. // Check what the mystery number is. (personal use for testing)
  23. // System.out.println(mysteryNum);
  24. // Empty variable for storing the previous guess to compare.
  25. // Conditionals (correct, too high, too low, same guess, out of bounds)
  26. if (mysteryNum == userGuess) {
  27. System.out.println("Correct! Number of attempts: " + userAttempts);
  28. break;
  29. } else if (userGuess > 100 || userGuess < 0) {
  30. System.out.println("Is that number between 0 and 100?");
  31. } else if (prevGuess == userGuess) {
  32. System.out.println("That's the same number!");
  33. userAttempts -= 1;
  34. } else if (mysteryNum > userGuess) {
  35. System.out.println("Too low! Try again!");
  36. } else if (mysteryNum < userGuess) {
  37. System.out.println("Too high! Try again!");
  38. }
  39. // Save previous guess for comparison.
  40. prevGuess = userGuess;
  41. }
  42. } while (mysteryNum != userGuess);
  43. }
  44. }